jQuery ESLint 美元($) 未定义。(无未定义)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/39510736/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 17:12:51  来源:igfitidea点击:

ESLint dollar($) is not defined. (no-undef)

jqueryeslint

提问by spbsmile

$("#ID").hide();

i add ESLint to my project .

我将 ESLint 添加到我的项目中。

everything is fine, except symbol $.

一切都很好,除了符号$

i get error: [eslint] '$' is not defined. (no-undef)

我得到错误: [eslint] '$' is not defined. (no-undef)

my .eslintrc.json(note: it has additional rulesset to disallow jquery functions when there's an equivalent javascript one):

我的.eslintrc.json(注意:当有一个等效的 javascript 函数时,它设置了额外的规则来禁止 jquery 函数):

{
"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"
    ]
}

you can see that I have added two plugins: eslint-plugin-dollar-signand eslint-plugin-jquery.

你可以看到我添加了两个插件:eslint-plugin-dollar-sign和 eslint-plugin-jquery。

why does not work this rule ?

为什么这条规则不起作用?

"dollar-sign/dollar-sign": [ 2, "ignoreProperties" ]

"dollar-sign/dollar-sign": [ 2, "ignoreProperties" ]

回答by Ilya Volodin

You are missing

你不见了

"env": {
  "browser": true,
  "commonjs": true,
  "es6": true,
  "jquery": true
},

$is not declared as a global without jqueryenvironment enabled. Because of that, you are getting a no-undeferror, saying that you are using variable that haven't been declared.

$在未jquery启用环境的情况下未声明为全局。因此,您会收到一个no-undef错误,说您正在使用尚未声明的变量。

回答by Frank Genova

https://eslint.org/docs/user-guide/configuring#specifying-environments

https://eslint.org/docs/user-guide/configuring#specifying-environments

You can specify environments using a comment inside of your JavaScript file, use the following format:

您可以使用 JavaScript 文件中的注释指定环境,使用以下格式:

Add the line below as a comment at the beginning of your JavaScript file.

在 JavaScript 文件的开头添加以下行作为注释。

    /*eslint-env jquery*/

The eslinter will stop throwing undefined on '$' because it will know you are working with jQuery.

eslinter 将停止在 '$' 上抛出 undefined,因为它会知道您正在使用 jQuery。

回答by ariel

You can also add this line to the top of your js file:

您还可以将此行添加到 js 文件的顶部:

/* global $ */

To prevent warning over "$", or for any other global like "varName":

为了防止对“$”或任何其他全局变量(如“varName”)发出警告:

/* global varName */

回答by Honghao Zhang