Home > Web Front-end > JS Tutorial > Solution to the error reported by Fundebug caused by import promotion (details)

Solution to the error reported by Fundebug caused by import promotion (details)

不言
Release: 2019-02-28 11:35:57
forward
2960 people have browsed it

The content of this article is about the solution to the Fundebug error caused by import promotion (details). It has certain reference value. Friends in need can refer to it. I hope it will be helpful to you.

Summary: Explain the reason for the "Please configure apikey" error.

When some Fundebug users use import to import js files, an error message "Please configure apikey" appears. This is caused by import promotion. I will explain this in detail below.

Import Improvement

Regarding import improvement, we can refer to Ruan Yifeng's "Introduction to ECMAScript 6".

The import command has a lifting effect and will be promoted to the head of the entire module and executed first.

foo();

import { foo } from 'my_module';
Copy after login

The above code will not report an error because import is executed earlier than foo is called. The essence of this behavior is that the import command is executed during the compilation phase, before the code is run.

Therefore, even if we write the import statement at the end, it will still be executed before other statements.

Why does import promotion cause Fundebug to report an error?

Fundebug users should be aware that after accessing the fundebug-javascript plug-in, they need to configure the apikey as follows:

import * as fundebug from "fundebug-javascript";
fundebug.apikey = "API-KEY";
Copy after login

Assume that we also need to import a test.js file, This file will throw an Error, as follows:

// test.js
throw new Error("test")
Copy after login

Everything looks fine:

// main.js
import * as fundebug from "fundebug-javascript";
fundebug.apikey = "API-KEY";
import "./test"
Copy after login

However, according to the import promotion, the actual execution order of the code is as follows:

// main.js
import * as fundebug from "fundebug-javascript";
import "./test"
fundebug.apikey = "API-KEY";
Copy after login

In this case, the second line of code will throw an error, causing the apikey copy statement to not be executed, resulting in an error: "Please configure apikey."

This problem does not need to be solved

For testing purposes, users will import a js file that reports an error immediately, similar to the test.js mentioned above. But in actual development, it is impossible for us to do this, otherwise the application will crash immediately, let alone deployment.

The purpose of our writing this blog is just to explain the reasons and share a very simple knowledge point "import promotion".

How to avoid this problem?

For reference only, there is actually no need to do this.

Create a new configuration file config.js, configure apikey in this file:

fundebug.apikey = "API-KEY";
Copy after login

import configuration file:

// main.js
import * as fundebug from "fundebug-javascript";
import "./config"
import "./test"
Copy after login

This kind In this case, the statement configuring apikey is replaced by import, so there is no so-called "import promotion" problem, and Fundebug will report errors normally.

The above is the detailed content of Solution to the error reported by Fundebug caused by import promotion (details). For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:segmentfault.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template