Javascript 和 ESLint 中的全局变量 [英] Global variables in Javascript and ESLint
问题描述
我有多个 javascript 文件,我在一个文件中定义了一些全局变量,该文件在其他文件之前加载.因此,在第一个之后加载的所有文件都可以访问全局变量.但是 ESLint 将全局变量显示为未定义".我不想改变 ESLint 的规则,我想找到一种优雅的方法来摆脱这些错误消息.有什么线索吗?谢谢
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
推荐答案
我不认为针对每个文件修改 ESLint 规则是个好主意.
I don't think hacking ESLint rules per file is a great idea.
您应该在 .eslintrc
或 package.json
中定义 globals
.
You should rather define globals
in .eslintrc
or package.json
.
对于.eslintrc:
For .eslintrc:
"globals": {
"angular": true
}
对于 package.json
:
"eslintConfig": {
"globals": {
"angular": true
}
}
检查 https://eslint.org/docs/user-guide/configuring/language-options#specifying-globals
这篇关于Javascript 和 ESLint 中的全局变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!