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

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

不言
不言forward
2019-02-28 11:35:572871browse

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';

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";

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")

Everything looks fine:

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

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";

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";

import configuration file:

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

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!

Statement:
This article is reproduced at:segmentfault.com. If there is any infringement, please contact admin@php.cn delete