$("#ID").hide();
Run Code Online (Sandbox Code Playgroud)
我将ESLint添加到我的项目中.
一切都很好,除了符号$.
我收到错误: [eslint] '$' is not defined. (no-undef)
我的.eslintrc.json:
{
"env": {
"browser": true,
"commonjs": true,
"es6": true
},
"extends": [
"eslint:recommended"
],
"parserOptions": {
"sourceType": "module"
},
"plugins": [
"dollar-sign",
"jquery"
],
"rules": {
"indent": [
"error" ,
"tab"
],
"linebreak-style": [
"error",
"windows"
],
"quotes": [
"error",
"double"
],
"semi": [
"error",
"always"
],
"jquery/no-ajax": 2,
"jquery/no-animate": 2,
"jquery/no-attr": 2,
"jquery/no-bind": 2,
"jquery/no-class": 2,
"jquery/no-clone": 2,
"jquery/no-closest": 2,
"jquery/no-css": 2,
"jquery/no-data": 2,
"jquery/no-deferred": 2,
"jquery/no-delegate": 2,
"jquery/no-each": 2,
"jquery/no-fade": 2,
"jquery/no-filter": 2,
"jquery/no-find": 2,
"jquery/no-global-eval": 2,
"jquery/no-has": 2,
"jquery/no-hide": 2,
"jquery/no-html": 2,
"jquery/no-in-array": 2,
"jquery/no-is": 2,
"jquery/no-map": 2,
"jquery/no-merge": 2,
"jquery/no-param": 2,
"jquery/no-parent": 2,
"jquery/no-parents": 2,
"jquery/no-parse-html": 2,
"jquery/no-prop": 2,
"jquery/no-proxy": 2,
"jquery/no-serialize": 2,
"jquery/no-show": 2,
"jquery/no-sizzle": 2,
"jquery/no-slide": 2,
"jquery/no-text": 2,
"jquery/no-toggle": 2,
"jquery/no-trigger": 2,
"jquery/no-trim": 2,
"jquery/no-val": 2,
"jquery/no-wrap": 2,
"dollar-sign/dollar-sign": [
2,
"ignoreProperties"
]
}
Run Code Online (Sandbox Code Playgroud)
你可以看到我添加了两个插件:eslint-plugin-dollar-sign和eslint-plugin-jquery.
为什么这个规则不起作用?
"dollar-sign/dollar-sign": [
2,
"ignoreProperties"
]
Ily*_*din 154
你错过了
"env": {
"browser": true,
"commonjs": true,
"es6": true,
"jquery": true
},
Run Code Online (Sandbox Code Playgroud)
$未jquery启用环境时未声明为全局.因此,您收到no-undef错误,说您正在使用尚未声明的变量.
小智 17
https://eslint.org/docs/user-guide/configuring#specifying-environments
您可以使用JavaScript文件中的注释指定环境,使用以下格式:
在JavaScript文件的开头添加以下行作为注释.
/*eslint-env jquery*/
Run Code Online (Sandbox Code Playgroud)
eslinter将停止在'$'上抛出undefined,因为它会知道你正在使用jQuery.
ari*_*iel 11
您还可以将此行添加到js文件的顶部:
/* global $ */
Run Code Online (Sandbox Code Playgroud)
要防止警告"$"或任何其他全局类似"varName":
/* global varName */
Run Code Online (Sandbox Code Playgroud)
在 .eslintrc.js
加
{
"globals": {
"$": true
}
}
Run Code Online (Sandbox Code Playgroud)
参见https://eslint.org/docs/user-guide/configuring#specifying-globals