I have got multiple javascript files and I have defined some global variable in a file which loads before the others. As a consequence all of the files loaded after the first have access to the global variable. However ESLint shows the global variable as "not defined". I don't want to change the rules of ESLint and I would like to find an elegant way to get rid of these error messages. Any clue? Thanks
我有多个javascript文件,我已经在一个文件中定义了一些全局变量,该文件在其他文件之前加载。因此,在第一个文件之后加载的所有文件都可以访问全局变量。但是,ESLint将全局变量显示为“未定义”。我不想改变ESLint的规则,我想找到一种优雅的方法来摆脱这些错误消息。任何线索?谢谢
2 个解决方案
#1
45
I don't think hacking ESLint rules per file is a great idea.
我不认为每个文件的黑客ESLint规则是一个好主意。
You should rather define globals
in .eslintrc
or package.json
.
您应该在.eslintrc或package.json中定义全局变量。
For .eslintrc:
"globals": {
"angular": true
}
For package.json
:
"eslintConfig": {
"globals": {
"angular": true
}
}
PS
Reading documentation is a virtue mandatory for developers
阅读文档对于开发人员来说是必须的
https://eslint.org/docs/user-guide/configuring#specifying-globals
#2
33
You can add globals either per file or in your config. If you don't want to change your config, you'll have to add the used globals in every file.
您可以按文件或配置添加全局变量。如果您不想更改配置,则必须在每个文件中添加使用过的全局变量。
To specify globals using a comment inside of your JavaScript file, use the following format:
要使用JavaScript文件中的注释指定全局变量,请使用以下格式:
/* global var1, var2 */
This defines two global variables,
var1
andvar2
. If you want to optionally specify that these global variables should never be written to (only read), then you can set each with a false flag:这定义了两个全局变量var1和var2。如果您希望有选择地指定永远不应该写入这些全局变量(仅读取),那么您可以使用false标志设置每个变量:
/* global var1:false, var2:false */
http://eslint.org/docs/2.0.0/user-guide/configuring#specifying-globals
#1
45
I don't think hacking ESLint rules per file is a great idea.
我不认为每个文件的黑客ESLint规则是一个好主意。
You should rather define globals
in .eslintrc
or package.json
.
您应该在.eslintrc或package.json中定义全局变量。
For .eslintrc:
"globals": {
"angular": true
}
For package.json
:
"eslintConfig": {
"globals": {
"angular": true
}
}
PS
Reading documentation is a virtue mandatory for developers
阅读文档对于开发人员来说是必须的
https://eslint.org/docs/user-guide/configuring#specifying-globals
#2
33
You can add globals either per file or in your config. If you don't want to change your config, you'll have to add the used globals in every file.
您可以按文件或配置添加全局变量。如果您不想更改配置,则必须在每个文件中添加使用过的全局变量。
To specify globals using a comment inside of your JavaScript file, use the following format:
要使用JavaScript文件中的注释指定全局变量,请使用以下格式:
/* global var1, var2 */
This defines two global variables,
var1
andvar2
. If you want to optionally specify that these global variables should never be written to (only read), then you can set each with a false flag:这定义了两个全局变量var1和var2。如果您希望有选择地指定永远不应该写入这些全局变量(仅读取),那么您可以使用false标志设置每个变量:
/* global var1:false, var2:false */
http://eslint.org/docs/2.0.0/user-guide/configuring#specifying-globals