vue關(guān)于eslint空格縮進(jìn)等的報錯問題及解決
關(guān)于eslint空格縮進(jìn)等的報錯問題
解決辦法有四種
1. 在.eslintrc.js文件中添加如下代碼:
rules: { 'no-console': process.env.NODE_ENV === 'production' ? 'warn' : 'off', 'no-debugger': process.env.NODE_ENV === 'production' ? 'warn' : 'off', //禁止console報錯檢查 'no-console': 'off', //禁止空格報錯檢查 'generator-star-spacing': 'off' }
2.在報錯的那個文件中的 里面第一行插入以下代碼:
/* eslint-disable */
這個最直接,忽視所有eslint規(guī)范(親測有效)
3.在最開始創(chuàng)建項目的時候,如果你不想有規(guī)范的js代碼,可以重新初始化關(guān)掉eslint,在這一步選擇No:
Use ESLint to lint your code? (Y/n) no
在bulid/webpack.base.conf.js里面有配置如下:
module: { rules: [ …(config.dev.useEslint ? [createLintingRule()] : []),
點進(jìn)config.dev.useEslint,發(fā)現(xiàn)在config/index.js里配置
useEslint: true, // 改為false即可。
(未嘗試)
4.打開vscode的設(shè)置,搜索eslint,在搜素出來的下面找到settings.json
如圖:
打開settings.json文件
修改代碼為如下:
{ "eslint.options": { "extensions": [ ".js", ".vue" ] }, "eslint.validate": [ "javascript", { "language": "vue", "autoFix": true }, "html", "vue" ], "eslint.autoFixOnSave": true, "editor.codeActionsOnSave": { "source.fixAll.eslint": true }, "eslint.codeAction.disableRuleComment": {}, "eslint.alwaysShowStatus": true, "workbench.colorTheme": "Monokai Dimmed", "eslint.codeAction.showDocumentation": { "enable": true }, }
eslint使用規(guī)則和各種報錯對應(yīng)規(guī)則
ESLint 主要有以下特點
- 默認(rèn)規(guī)則包含所有 JSLint、JSHint 中存在的規(guī)則,易遷移;
- 規(guī)則可配置性高:可設(shè)置「警告」、「錯誤」兩個 error 等級,或者直接禁用;
- 包含代碼風(fēng)格檢測的規(guī)則(可以丟掉 JSCS 了);
- 支持插件擴展、自定義規(guī)則
下面說一下如何配置:
比如,在寫vue過程中,出現(xiàn)了下面的warning:
WARNING Compiled with 1 warnings 11:26:30 http://eslint.org/docs/rules/quotes Strings must use singlequote
警告的意思就是字符串必須用單引號
這時候,你就可以在.eslintrc.js配置你的規(guī)則rule
"quotes": [1, "single"],//引號類型 `` "" ''
還有一種萬能方法,就是在報錯的JS文件中第一行寫上
/* eslint-disable */
這樣該行代碼就不會執(zhí)行eslint代碼檢測了。
配置(我主要用第三種方法,為了方便查看,特記錄如下:)
可以通過以下三種方式配置 ESLint:
- 使用 .eslintrc 文件(支持 JSON 和 YAML 兩種語法);
- 在 package.json 中添加 eslintConfig 配置塊;直接在代碼文件中定義。
- 以下是.eslintrc 文件示例和解釋:為了方便查看,特記錄如下
{ "env": { "browser": true, "node": true, "commonjs": true }, "ecmaFeatures": { // lambda表達(dá)式 "arrowFunctions": true, // 解構(gòu)賦值 "destructuring": true, // class "classes": true, // http://es6.ruanyifeng.com/#docs/function#函數(shù)參數(shù)的默認(rèn)值 "defaultParams": true, // 塊級作用域,允許使用let const "blockBindings": true, // 允許使用模塊,模塊內(nèi)默認(rèn)嚴(yán)格模式 "modules": true, // 允許字面量定義對象時,用表達(dá)式做屬性名 // http://es6.ruanyifeng.com/#docs/object#屬性名表達(dá)式 "objectLiteralComputedProperties": true, // 允許對象字面量方法名簡寫 /*var o = { method() { return "Hello!"; } }; 等同于 var o = { method: function() { return "Hello!"; } }; */ "objectLiteralShorthandMethods": true, /* 對象字面量屬性名簡寫 var foo = 'bar'; var baz = {foo}; baz // {foo: "bar"} // 等同于 var baz = {foo: foo}; */ "objectLiteralShorthandProperties": true, // http://es6.ruanyifeng.com/#docs/function#rest參數(shù) "restParams": true, // http://es6.ruanyifeng.com/#docs/function#擴展運算符 "spread": true, // http://es6.ruanyifeng.com/#docs/iterator#for---of循環(huán) "forOf": true, // http://es6.ruanyifeng.com/#docs/generator "generators": true, // http://es6.ruanyifeng.com/#docs/string#模板字符串 "templateStrings": true, "superInFunctions": true, // http://es6.ruanyifeng.com/#docs/object#對象的擴展運算符 "experimentalObjectRestSpread": true }, "rules": { // 定義對象的set存取器屬性時,強制定義get "accessor-pairs": 2, // 指定數(shù)組的元素之間要以空格隔開(,后面), // never參數(shù):[ 之前和 ] 之后不能帶空格,always參數(shù):[ 之前和 ] 之后必須帶空格 "array-bracket-spacing": [2, "never"], // 在塊級作用域外訪問塊內(nèi)定義的變量是否報錯提示 "block-scoped-var": 0, // if while function 后面的{必須與if在同一行,java風(fēng)格。 "brace-style": [2, "1tbs", { "allowSingleLine": true }], // 雙峰駝命名格式 "camelcase": 2, // 數(shù)組和對象鍵值對最后一個逗號, never參數(shù):不能帶末尾的逗號, // always參數(shù):必須帶末尾的逗號, // always-multiline:多行模式必須帶逗號,單行模式不能帶逗號 "comma-dangle": [2, "never"], // 控制逗號前后的空格 "comma-spacing": [2, { "before": false, "after": true }], // 控制逗號在行尾出現(xiàn)還是在行首出現(xiàn) // http://eslint.org/docs/rules/comma-style "comma-style": [2, "last"], // 圈復(fù)雜度 "complexity": [2,9], // 以方括號取對象屬性時,[ 后面和 ] 前面是否需要空格, // 可選參數(shù) never, always "computed-property-spacing": [2,"never"], // 強制方法必須返回值,TypeScript強類型,不配置 "consistent-return": 0, // 用于指統(tǒng)一在回調(diào)函數(shù)中指向this的變量名, // 箭頭函數(shù)中的this已經(jīng)可以指向外層調(diào)用者,應(yīng)該沒卵用了 // [0,"that"] 指定只能 var that = this. // that不能指向其他任何值,this也不能賦值給that以外的其他值 "consistent-this": 0, // 強制在子類構(gòu)造函數(shù)中用super()調(diào)用父類構(gòu)造函數(shù),TypeScrip的編譯器也會提示 "constructor-super": 0, // if else while for do后面的代碼塊是否需要{ }包圍,參數(shù): // multi 只有塊中有多行語句時才需要{ }包圍 // multi-line 只有塊中有多行語句時才需要{ }包圍, 但是塊中的執(zhí)行語句只有一行時, // 塊中的語句只能跟和if語句在同一行。if (foo) foo++; else doSomething(); // multi-or-nest 只有塊中有多行語句時才需要{ }包圍, 如果塊中的執(zhí)行語句只有一行,執(zhí)行語句可以零另起一行也可以跟在if語句后面 // [2, "multi", "consistent"] 保持前后語句的{ }一致 // default: [2, "all"] 全都需要{ }包圍 "curly": [2, "all"], // switch語句強制default分支,也可添加 // no default 注釋取消此次警告 "default-case": 2, // 強制object.key 中 . 的位置,參數(shù): // property,'.'號應(yīng)與屬性在同一行 // object, '.' 號應(yīng)與對象名在同一行 "dot-location": [2, "property"], // 強制使用.號取屬性 // 參數(shù): allowKeywords true 使用保留字做屬性名時,只能使用.方式取屬性 // false 使用保留字做屬性名時, 只能使用[]方式取屬性 e.g [2, {"allowKeywords": false}] // allowPattern: 當(dāng)屬性名匹配提供的正則表達(dá)式時,允許使用[]方式取值,否則只能用.號取值 e.g [2, {"allowPattern": "^[a-z]+(_[a-z]+)+$"}] "dot-notation": [2, {"allowKeywords": true}], // 文件末尾強制換行 "eol-last": 2, // 使用 === 替代 == "eqeqeq": [2, "allow-null"], // 方法表達(dá)式是否需要命名 "func-names": 0, // 方法定義風(fēng)格,參數(shù): // declaration: 強制使用方法聲明的方式,function f(){} // expression:強制使用方法表達(dá)式的方式,var f = function() {} // allowArrowFunctions: declaration風(fēng)格中允許箭頭函數(shù)。 "func-style": 0, "no-alert": 0,//禁止使用alert confirm prompt "no-array-constructor": 2,//禁止使用數(shù)組構(gòu)造器 "no-bitwise": 0,//禁止使用按位運算符 "no-caller": 1,//禁止使用arguments.caller或arguments.callee "no-catch-shadow": 2,//禁止catch子句參數(shù)與外部作用域變量同名 "no-class-assign": 2,//禁止給類賦值 "no-cond-assign": 2,//禁止在條件表達(dá)式中使用賦值語句 "no-console": 2,//禁止使用console "no-const-assign": 2,//禁止修改const聲明的變量 "no-constant-condition": 2,//禁止在條件中使用常量表達(dá)式 if(true) if(1) "no-continue": 0,//禁止使用continue "no-control-regex": 2,//禁止在正則表達(dá)式中使用控制字符 "no-debugger": 2,//禁止使用debugger "no-delete-var": 2,//不能對var聲明的變量使用delete操作符 "no-div-regex": 1,//不能使用看起來像除法的正則表達(dá)式/=foo/ "no-dupe-keys": 2,//在創(chuàng)建對象字面量時不允許鍵重復(fù) {a:1,a:1} "no-dupe-args": 2,//函數(shù)參數(shù)不能重復(fù) "no-duplicate-case": 2,//switch中的case標(biāo)簽不能重復(fù) "no-else-return": 2,//如果if語句里面有return,后面不能跟else語句 "no-empty": 2,//塊語句中的內(nèi)容不能為空 "no-empty-character-class": 2,//正則表達(dá)式中的[]內(nèi)容不能為空 "no-empty-label": 2,//禁止使用空label "no-eq-null": 2,//禁止對null使用==或!=運算符 "no-eval": 1,//禁止使用eval "no-ex-assign": 2,//禁止給catch語句中的異常參數(shù)賦值 "no-extend-native": 2,//禁止擴展native對象 "no-extra-bind": 2,//禁止不必要的函數(shù)綁定 "no-extra-boolean-cast": 2,//禁止不必要的bool轉(zhuǎn)換 "no-extra-parens": 2,//禁止非必要的括號 "no-extra-semi": 2,//禁止多余的冒號 "no-fallthrough": 1,//禁止switch穿透 "no-floating-decimal": 2,//禁止省略浮點數(shù)中的0 .5 3. "no-func-assign": 2,//禁止重復(fù)的函數(shù)聲明 "no-implicit-coercion": 1,//禁止隱式轉(zhuǎn)換 "no-implied-eval": 2,//禁止使用隱式eval "no-inline-comments": 0,//禁止行內(nèi)備注 "no-inner-declarations": [2, "functions"],//禁止在塊語句中使用聲明(變量或函數(shù)) "no-invalid-regexp": 2,//禁止無效的正則表達(dá)式 "no-invalid-this": 2,//禁止無效的this,只能用在構(gòu)造器,類,對象字面量 "no-irregular-whitespace": 2,//不能有不規(guī)則的空格 "no-iterator": 2,//禁止使用__iterator__ 屬性 "no-label-var": 2,//label名不能與var聲明的變量名相同 "no-labels": 2,//禁止標(biāo)簽聲明 "no-lone-blocks": 2,//禁止不必要的嵌套塊 "no-lonely-if": 2,//禁止else語句內(nèi)只有if語句 "no-loop-func": 1,//禁止在循環(huán)中使用函數(shù)(如果沒有引用外部變量不形成閉包就可以) "no-mixed-requires": [0, false],//聲明時不能混用聲明類型 "no-mixed-spaces-and-tabs": [2, false],//禁止混用tab和空格 "linebreak-style": [0, "windows"],//換行風(fēng)格 "no-multi-spaces": 1,//不能用多余的空格 "no-multi-str": 2,//字符串不能用\換行 "no-multiple-empty-lines": [1, {"max": 2}],//空行最多不能超過2行 "no-native-reassign": 2,//不能重寫native對象 "no-negated-in-lhs": 2,//in 操作符的左邊不能有! "no-nested-ternary": 0,//禁止使用嵌套的三目運算 "no-new": 1,//禁止在使用new構(gòu)造一個實例后不賦值 "no-new-func": 1,//禁止使用new Function "no-new-object": 2,//禁止使用new Object() "no-new-require": 2,//禁止使用new require "no-new-wrappers": 2,//禁止使用new創(chuàng)建包裝實例,new String new Boolean new Number "no-obj-calls": 2,//不能調(diào)用內(nèi)置的全局對象,比如Math() JSON() "no-octal": 2,//禁止使用八進(jìn)制數(shù)字 "no-octal-escape": 2,//禁止使用八進(jìn)制轉(zhuǎn)義序列 "no-param-reassign": 2,//禁止給參數(shù)重新賦值 "no-path-concat": 0,//node中不能使用__dirname或__filename做路徑拼接 "no-plusplus": 0,//禁止使用++,-- "no-process-env": 0,//禁止使用process.env "no-process-exit": 0,//禁止使用process.exit() "no-proto": 2,//禁止使用__proto__屬性 "no-redeclare": 2,//禁止重復(fù)聲明變量 "no-regex-spaces": 2,//禁止在正則表達(dá)式字面量中使用多個空格 /foo bar/ "no-restricted-modules": 0,//如果禁用了指定模塊,使用就會報錯 "no-return-assign": 1,//return 語句中不能有賦值表達(dá)式 "no-script-url": 0,//禁止使用javascript:void(0) "no-self-compare": 2,//不能比較自身 "no-sequences": 0,//禁止使用逗號運算符 "no-shadow": 2,//外部作用域中的變量不能與它所包含的作用域中的變量或參數(shù)同名 "no-shadow-restricted-names": 2,//嚴(yán)格模式中規(guī)定的限制標(biāo)識符不能作為聲明時的變量名使用 "no-spaced-func": 2,//函數(shù)調(diào)用時 函數(shù)名與()之間不能有空格 "no-sparse-arrays": 2,//禁止稀疏數(shù)組, [1,,2] "no-sync": 0,//nodejs 禁止同步方法 "no-ternary": 0,//禁止使用三目運算符 "no-trailing-spaces": 1,//一行結(jié)束后面不要有空格 "no-this-before-super": 0,//在調(diào)用super()之前不能使用this或super "no-throw-literal": 2,//禁止拋出字面量錯誤 throw "error"; "no-undef": 1,//不能有未定義的變量 "no-undef-init": 2,//變量初始化時不能直接給它賦值為undefined "no-undefined": 2,//不能使用undefined "no-unexpected-multiline": 2,//避免多行表達(dá)式 "no-underscore-dangle": 1,//標(biāo)識符不能以_開頭或結(jié)尾 "no-unneeded-ternary": 2,//禁止不必要的嵌套 var isYes = answer === 1 ? true : false; "no-unreachable": 2,//不能有無法執(zhí)行的代碼 "no-unused-expressions": 2,//禁止無用的表達(dá)式 //不能有聲明后未被使用的變量或參數(shù) "no-unused-vars": [2, {"vars": "all", "args": "after-used"}], "no-use-before-define": 2,//未定義前不能使用 "no-useless-call": 2,//禁止不必要的call和apply "no-void": 2,//禁用void操作符 "no-var": 0,//禁用var,用let和const代替 "no-warning-comments": [1, { "terms": ["todo", "fixme", "xxx"], "location": "start" }], "no-with": 2,//禁用with "array-bracket-spacing": [2, "never"],//是否允許非空數(shù)組里面有多余的空格 "arrow-parens": 0,//箭頭函數(shù)用小括號括起來 "arrow-spacing": 0,//=>的前/后括號 "accessor-pairs": 0,//在對象中使用getter/setter "block-scoped-var": 0,//塊語句中使用var "brace-style": [1, "1tbs"],//大括號風(fēng)格 "callback-return": 1,//避免多次調(diào)用回調(diào)什么的 "camelcase": 2,//強制駝峰法命名 "comma-dangle": [2, "never"],//對象字面量項尾不能有逗號 "comma-spacing": 0,//逗號前后的空格 "comma-style": [2, "last"],//逗號風(fēng)格,換行時在行首還是行尾 "complexity": [0, 11],//循環(huán)復(fù)雜度 "computed-property-spacing": [0, "never"],//是否允許計算后的鍵名什么的 "consistent-return": 0,//return 后面是否允許省略 "consistent-this": [2, "that"],//this別名 "constructor-super": 0,//非派生類不能調(diào)用super,派生類必須調(diào)用super "curly": [2, "all"],//必須使用 if(){} 中的{} "default-case": 2,//switch語句最后必須有default "dot-location": 0,//對象訪問符的位置,換行的時候在行首還是行尾 "dot-notation": [0, { "allowKeywords": true }],//避免不必要的方括號 "eol-last": 0,//文件以單一的換行符結(jié)束 "eqeqeq": 2,//必須使用全等 "func-names": 0,//函數(shù)表達(dá)式必須有名字 "func-style": [0, "declaration"],//函數(shù)風(fēng)格,規(guī)定只能使用函數(shù)聲明/函數(shù)表達(dá)式 "generator-star-spacing": 0,//生成器函數(shù)*的前后空格 "guard-for-in": 0,//for in循環(huán)要用if語句過濾 "handle-callback-err": 0,//nodejs 處理錯誤 "id-length": 0,//變量名長度 "indent": [2, 4],//縮進(jìn)風(fēng)格 "init-declarations": 0,//聲明時必須賦初值 "key-spacing": [0, { "beforeColon": false, "afterColon": true }],//對象字面量中冒號的前后空格 "lines-around-comment": 0,//行前/行后備注 "max-depth": [0, 4],//嵌套塊深度 "max-len": [0, 80, 4],//字符串最大長度 "max-nested-callbacks": [0, 2],//回調(diào)嵌套深度 "max-params": [0, 3],//函數(shù)最多只能有3個參數(shù) "max-statements": [0, 10],//函數(shù)內(nèi)最多有幾個聲明 "new-cap": 2,//函數(shù)名首行大寫必須使用new方式調(diào)用,首行小寫必須用不帶new方式調(diào)用 "new-parens": 2,//new時必須加小括號 "newline-after-var": 2,//變量聲明后是否需要空一行 "object-curly-spacing": [0, "never"],//大括號內(nèi)是否允許不必要的空格 "object-shorthand": 0,//強制對象字面量縮寫語法 "one-var": 1,//連續(xù)聲明 "operator-assignment": [0, "always"],//賦值運算符 += -=什么的 "operator-linebreak": [2, "after"],//換行時運算符在行尾還是行首 "padded-blocks": 0,//塊語句內(nèi)行首行尾是否要空行 "prefer-const": 0,//首選const "prefer-spread": 0,//首選展開運算 "prefer-reflect": 0,//首選Reflect的方法 "quotes": [1, "single"],//引號類型 `` "" '' "quote-props":[2, "always"],//對象字面量中的屬性名是否強制雙引號 "radix": 2,//parseInt必須指定第二個參數(shù) "id-match": 0,//命名檢測 "require-yield": 0,//生成器函數(shù)必須有yield "semi": [2, "always"],//語句強制分號結(jié)尾 "semi-spacing": [0, {"before": false, "after": true}],//分號前后空格 "sort-vars": 0,//變量聲明時排序 "space-after-keywords": [0, "always"],//關(guān)鍵字后面是否要空一格 "space-before-blocks": [0, "always"],//不以新行開始的塊{前面要不要有空格 "space-before-function-paren": [0, "always"],//函數(shù)定義時括號前面要不要有空格 "space-in-parens": [0, "never"],//小括號里面要不要有空格 "space-infix-ops": 0,//中綴操作符周圍要不要有空格 "space-return-throw-case": 2,//return throw case后面要不要加空格 "space-unary-ops": [0, { "words": true, "nonwords": false }],//一元運算符的前/后要不要加空格 "spaced-comment": 0,//注釋風(fēng)格不要有空格什么的 "strict": 2,//使用嚴(yán)格模式 "use-isnan": 2,//禁止比較時使用NaN,只能用isNaN() "valid-jsdoc": 0,//jsdoc規(guī)則 "valid-typeof": 2,//必須使用合法的typeof的值 "vars-on-top": 2,//var必須放在作用域頂部 "wrap-iife": [2, "inside"],//立即執(zhí)行函數(shù)表達(dá)式的小括號風(fēng)格 "wrap-regex": 0,//正則表達(dá)式字面量用小括號包起來 "yoda": [2, "never"]//禁止尤達(dá)條件 } }
以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
- Vue3配置路由ERROR in [eslint]報錯問題及解決
- vue?eslint報錯error?"Component?name?"*****"?should?always?be?multi-word"解決
- vue?eslint報錯:Component?name?“xxxxx“?should?always?be?multi-word.eslintvue的4種解決方案
- vue-cli創(chuàng)建項目時由esLint校驗導(dǎo)致報錯或警告的問題及解決
- vue?cli2?和?cli3去掉eslint檢查器報錯的解決
- vue項目下,如何用命令直接修復(fù)ESLint報錯
- vue中eslint導(dǎo)致的報錯問題及解決
相關(guān)文章
詳解關(guān)于element級聯(lián)選擇器數(shù)據(jù)回顯問題
這篇文章主要介紹了詳解關(guān)于element級聯(lián)選擇器數(shù)據(jù)回顯問題,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2019-02-02Vue3中Composition?API和Options?API的區(qū)別
Vue3的Composition API和Options API是Vue.js框架中的兩種不同的API,本文主要介紹了Vue3中Composition?API和Options?API的區(qū)別,文中通過示例代碼介紹的非常詳細(xì),需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2023-06-06關(guān)于引入vue.js 文件的知識點總結(jié)
在本篇文章里小編給大家分享的是關(guān)于引入vue.js 文件的知識點總結(jié),有需要的朋友們可以參考學(xué)習(xí)下。2020-01-01vue-router 按需加載 component: () => import() 報錯的解決
這篇文章主要介紹了vue-router 按需加載 component: () => import() 報錯的解決,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-09-09