1、問題javascript
出現這麼寫錯誤是什麼緣由呢?相信不少小白都會像我同樣,第一次接觸時有點二丈和尚摸不着頭腦。實際上是在你用vue-cli腳手架構建項目時用了ESLint代碼檢查工具,以下圖vue
那麼什麼是ESLint呢?java
2、ESLint介紹(中文官網)node
官網是這用介紹的,webpack
ESLint 是一個開源的 JavaScript 代碼檢查工具,由 Nicholas C. Zakas 於2013年6月建立。代碼檢查是一種靜態的分析,經常使用於尋找有問題的模式或者代碼,而且不依賴於具體的編碼風格。對大多數編程語言來講都會有代碼檢查,通常來講編譯程序會內置檢查工具。程序員
JavaScript 是一個動態的弱類型語言,在開發中比較容易出錯。由於沒有編譯程序,爲了尋找 JavaScript 代碼錯誤一般須要在執行過程當中不斷調適。像 ESLint 這樣的可讓程序員在編碼的過程當中發現問題而不是在執行的過程當中。web
ESLint 的初衷是爲了讓程序員能夠建立本身的檢測規則。ESLint 的全部規則都被設計成可插入的。ESLint 的默認規則與其餘的插件並無什麼區別,規則自己和測試能夠依賴於一樣的模式。爲了便於人們使用,ESLint 內置了一些規則,固然,你能夠在使用過程當中自定義規則。正則表達式
ESLint 使用 Node.js 編寫,這樣既能夠有一個快速的運行環境的同時也便於安裝。vue-cli
說白了,他就是一個代碼檢查工具,用來規範你的代碼的。express
使用代碼檢查的重要性
3、問題解決
在目錄中咱們能夠看到".eslintrc.js "文件
找到文件中的rules,咱們能夠在其中定義一些代碼檢查的規則
例如:
1.
,說的是我在 } 後面多加了一個分號 「;」。這是咱們在rules中加了一條規則
2.
,說我在mian.js文件結尾應添加一個換行,
解決方法:那就添加一個換行咯。或者在rules中加入規則 'eol-last': 0
3.
,這裏個人App.vue中代碼 'v-header':header之間少一個空格
,那就加一個空格咯,
4.
,註釋內容與’//‘之間須要一個空格
好了,再看看命令窗口已經不報錯了
在webpack.base.conf.js裏面刪掉下面:
preLoaders: [
{
test: /\.vue$/, loader: 'eslint', include: projectRoot, exclude: [/node_modules/, /ignore_lib/] }, { test: /\.js$/, loader: 'eslint', include: projectRoot, exclude: [/node_modules/, /ignore_lib/] } ]
或者不想進行校驗的文件就想上面同樣建立一個ignore_lib,而後所有丟進去
5、經常使用規則
有時候代碼裏有些特殊狀況須要咱們在某一行或者某幾行關閉ESLint檢測,可使用註釋:
'rules': { "comma-dangle": ["error", "never"], //是否容許對象中出現結尾逗號 "no-cond-assign": 2, //條件語句的條件中不容許出現賦值運算符 "no-console": 2, //不容許出現console語句 "no-constant-condition": 2, //條件語句的條件中不容許出現恆定不變的量 "no-control-regex": 2, //正則表達式中不容許出現控制字符 "no-debugger": 2, //不容許出現debugger語句 "no-dupe-args": 2, //函數定義的時候不容許出現重複的參數 "no-dupe-keys": 2, //對象中不容許出現重複的鍵 "no-duplicate-case": 2, //switch語句中不容許出現重複的case標籤 "no-empty": 2, //不容許出現空的代碼塊 "no-empty-character-class": 2, //正則表達式中不容許出現空的字符組 "no-ex-assign": 2, //在try catch語句中不容許從新分配異常變量 "no-extra-boolean-cast": 2, //不容許出現沒必要要的布爾值轉換 "no-extra-parens": 0, //不容許出現沒必要要的圓括號 "no-extra-semi": 2, //不容許出現沒必要要的分號 "no-func-assign": 2, //不容許從新分配函數聲明 "no-inner-declarations": ["error", "functions"], //不容許在嵌套代碼塊裏聲明函數 "no-invalid-regexp": 2, //不容許在RegExp構造函數裏出現無效的正則表達式 "no-irregular-whitespace": 2, //不容許出現不規則的空格 "no-negated-in-lhs": 2, //不容許在in表達式語句中對最左邊的運算數使用取反操做 "no-obj-calls": 2, //不容許把全局對象屬性當作函數來調用 "no-regex-spaces": 2, //正則表達式中不容許出現多個連續空格 "quote-props": 2, //對象中的屬性名是否須要用引號引發來 "no-sparse-arrays": 2, //數組中不容許出現空位置 "no-unreachable": 2, //在return,throw,continue,break語句後不容許出現不可能到達的語句 "use-isnan": 2, //要求檢查NaN的時候使用isNaN() "valid-jsdoc": ["error", { "requireReturn": false, "requireParamDescription": false, "requireReturnDescription": true }], //強制JSDoc註釋 "valid-typeof": ["error", { "requireStringLiterals": true }], //在使用typeof表達式比較的時候強制使用有效的字符串 "block-scoped-var": 2, //將變量聲明放在合適的代碼塊裏 "complexity": 0, //限制條件語句的複雜度 "consistent-return": 2, //不管有沒有返回值都強制要求return語句返回一個值 "curly": ["error", "all"], //強制使用花括號的風格 "default-case": 0, //在switch語句中須要有default語句 "dot-notation": ["error", {"allowKeywords": false, "allowPattern": ""}], //獲取對象屬性的時候使用點號 "eqeqeq": ["error", "smart"], //比較的時候使用嚴格等於 "no-alert": 1, //不容許使用alert,confirm,prompt語句 "no-caller": 2, //不容許使用arguments.callee和arguments.caller屬性 "guard-for-in": 0, //監視for in循環,防止出現不可預料的狀況 "no-div-regex": 2, //不能使用看起來像除法的正則表達式 "no-else-return": 0, //若是if語句有return,else裏的return不用放在else裏 "no-labels": ["error", { "allowLoop": false, "allowSwitch": false }], //不容許標籤語句 "no-eq-null": 2, //不容許對null用==或者!= "no-eval": 2, //不容許使用eval() "no-extend-native": 2, //不容許擴展原生對象 "no-extra-bind": 2, //不容許沒必要要的函數綁定 "no-fallthrough": 2, //不容許switch按順序所有執行全部case "no-floating-decimal": 2, //不容許浮點數缺失數字 "no-implied-eval": 2, //不容許使用隱式eval() "no-iterator": 2, //不容許使用__iterator__屬性 "no-lone-blocks": 2, //不容許沒必要要的嵌套代碼塊 "no-loop-func": 2, //不容許在循環語句中進行函數聲明 "no-multi-spaces": 2, //不容許出現多餘的空格 "no-multi-str": 2, //不容許用\來讓字符串換行 "no-global-assign": 2, //不容許從新分配原生對象 "no-new": 2, //不容許new一個實例後不賦值或者不比較 "no-new-func": 2, //不容許使用new Function "no-new-wrappers": 2, //不容許使用new String,Number和Boolean對象 "no-octal": 2, //不容許使用八進制字面值 "no-octal-escape": 2, //不容許使用八進制轉義序列 "no-param-reassign": 0, //不容許從新分配函數參數"no-proto": 2, //不容許使用__proto__屬性 "no-redeclare": 2, //不容許變量重複聲明 "no-return-assign": 2, //不容許在return語句中使用分配語句 "no-script-url": 2, //不容許使用javascript:void(0) "no-self-compare": 2, //不容許本身和本身比較 "no-sequences": 2, //不容許使用逗號表達式 "no-throw-literal": 2, //不容許拋出字面量錯誤 throw "error" "no-unused-expressions": 2, //不容許無用的表達式 "no-void": 2, //不容許void操做符 "no-warning-comments": [1, {"terms": ["todo", "fixme", "any other term"]}], //不容許警告備註 "no-with": 2, //不容許使用with語句 "radix": 1, //使用parseInt時強制使用基數來指定是十進制仍是其餘進制 "vars-on-top": 0, //var必須放在做用域頂部 "wrap-iife": [2, "any"], //當即執行表達式的括號風格 "yoda": [2, "never", {"exceptRange": true}], //不容許在if條件中使用yoda條件 "strict": [2, "function"], //使用嚴格模式 "no-catch-shadow": 2, //不容許try catch語句接受的err變量與外部變量重名"no-delete-var": 2, //不容許使用delete操做符 "no-label-var": 2, //不容許標籤和變量同名 "no-shadow": 2, //外部做用域中的變量不能與它所包含的做用域中的變量或參數同名 "no-shadow-restricted-names": 2, //js關鍵字和保留字不能做爲函數名或者變量名 "no-undef": 2, //不容許未聲明的變量 "no-undef-init": 2, //不容許初始化變量時給變量賦值undefined "no-undefined": 2, //不容許把undefined當作標識符使用 "no-unused-vars": [2, {"vars": "all", "args": "after-used"}], //不容許有聲明後未使用的變量或者參數 "no-use-before-define": [2, "nofunc"], //不容許在未定義以前就使用變量"indent": 2, //強制一致的縮進風格 "brace-style": [2, "1tbs", { "allowSingleLine": false}], //大括號風格 "camelcase": [2, {"properties": "never"}], //強制駝峯命名規則 "comma-style": [2, "last"], //逗號風格 "consistent-this": [0, "self"], //當獲取當前環境的this是用同樣的風格 "eol-last": 2, //文件以換行符結束 "func-names": 0, //函數表達式必須有名字 "func-style": 0, //函數風格,規定只能使用函數聲明或者函數表達式 "key-spacing": [2, {"beforeColon": false, "afterColon": true}], //對象字面量中冒號的先後空格 "max-nested-callbacks": 0, //回調嵌套深度 "new-cap": [2, {"newIsCap": true, "capIsNew": false}], //構造函數名字首字母要大寫 "new-parens": 2, //new時構造函數必須有小括號 "newline-after-var": 0, //變量聲明後必須空一行 "no-array-constructor": 2, //不容許使用數組構造器 "no-inline-comments": 0, //不容許行內註釋 "no-lonely-if": 0, //不容許else語句內只有if語句 "no-mixed-spaces-and-tabs": [2, "smart-tabs"], //不容許混用tab和空格 "no-multiple-empty-lines": [2, {"max": 2}], //空行最多不能超過兩行 "no-nested-ternary": 2, //不容許使用嵌套的三目運算符 "no-new-object": 2, //禁止使用new Object() "fun-call-spacing": 2, //函數調用時,函數名與()之間不能有空格 "no-ternary": 0, //不容許使用三目運算符 "no-trailing-spaces": 2, //一行最後不容許有空格 "no-underscore-dangle": 2, //不容許標識符如下劃線開頭 "no-extra-parens": 0, //不容許出現多餘的括號 "one-var": 0, //強制變量聲明放在一塊兒 "operator-assignment": 0, //賦值運算符的風格 "padded-blocks": [2, "never"], //塊內行首行尾是否空行 "quote-props": 0, //對象字面量中屬性名加引號 "quotes": [1, "single", "avoid-escape"], //引號風格 "semi": [2, "always"], //強制語句分號結尾 "semi-spacing": [2, {"before": false, "after": true}], //分後先後空格 "sort-vars": 0, //變量聲明時排序 "space-before-blocks": [2, "always"], //塊前的空格 "space-before-function-paren": [2, {"anonymous": "always", "named": "never"}], //函數定義時括號前的空格 "space-infix-ops": [2, {"int32Hint": true}], //操做符周圍的空格 "keyword-spacing": 2, //關鍵字先後的空格 "space-unary-ops": [2, { "words": true, "nonwords": false}], //一元運算符先後不要加空格 "wrap-regex": 2, //正則表達式字面量用括號括起來 "no-var": 0, //使用let和const代替var "generator-star-spacing": [2, "both"], //生成器函數先後空格 "max-depth": 0, //嵌套塊深度 "max-len": 0, //一行最大長度,單位爲字符 "max-params": 0, //函數最多能有多少個參數 "max-statements": 0, //函數內最多有幾個聲明 "no-bitwise": 0, //不容許使用位運算符 "no-plusplus": 0 //不容許使用++ --運算符 }
屬性名 | 屬性值 | 描述 |
array-callback-return | "error" | Array執行回調函數返回語句 |
indent | ["error", 4, {"SwitchCase": 1}] | 縮寫格式的一致性 |
block-spacing | "error" | 禁止執行空間內出現'-' |
brace-style | ["error","1tbs"] | 代碼書寫格式驗證 |
camelcase | ["error", { "properties": "never" }] | 屬性命名規則能夠不使用駝峯命名法 |
callback-return | ["error", ["cb", "callback", "next"]] | 回調函數須要return進行返回 |
comma-spacing | "error" | 不容許在逗號前面出現空格 |
comma-style | ["error", "last"] | 方數組元素、變量聲明等直接須要逗號隔開 |
consistent-return | "error" | 保持return返回的一致性 |
curly | ["error", "all"] | 函數或者條件判斷時須要統一使用大括號 |
default-case | "error" | switch語句中必須有default條件 |
dot-notation | ["error", { "allowKeywords": false }] | 不容許關鍵字出如今變量中 |
eol-last | "error" | 代碼間間隔出現一行 |
eqeqeq | "error" | 消除不安全類型的全等操做 |
guard-for-in | "error" | for循環中過濾掉一下不被須要的行爲 |
key-spacing | ["error", { "beforeColon": false, "afterColon": true }] | 鍵和值前保留一個空格 |
keyword-spacing | "error" | 確保字符先後空格的一致性 |
lines-around-comment | ["error", { "beforeBlockComment": true, "afterBlockComment": false, "beforeLineComment": true, "afterLineComment": false }] |
註釋前須要空行,註釋後不須要空行 |
new-cap | "error" | 構造函數首字母須要大寫 |
newline-after-var | ["error", "never"] | var定義後不空行 |
new-parens | "error" | 沒有參數時,構造函數也須要添加括號 |
no-invalid-this | "error" | 不容許關鍵字this在函數或者類的外面 |
no-multi-spaces | "error" | 不容許鍵和值之間存在多個空格 |
no-redeclare | "error" | 不容許重複聲明 |
no-return-assign | "error" | 不容許在return語句中任務 |
no-spaced-func | "error" | 調用函數時,函數名和括號之間不能有空格。 |
no-trailing-spaces | "error" | 不容許在語句後存在多餘的空格 |
semi | "error" | 語句以分號結尾 |
semi-spacing | "error" | 分號先後不能有空格 |
quotes | ["error","double"] | 使用雙引號 |
[space-before-function-paren](http://eslint.org/docs/rules/space-before-function-paren) | "space-before-function-paren": ["error", "never"] | 不容許函數括號之間存在空格 |
space-in-parens | "error" | 不容許在括號裏面存在空格 |
space-infix-ops | "error" | 插入符合變量之間須要添加一個空格 |
space-unary-ops | ["error", {"words": true, "nonwords": false}] | 容許一元運算符操做 |
spaced-comment | "error" | 註釋前須要一個空格 |
yoda | ["error", "never"] | 條件語句中,變量在賦值語句的前面 |
no-mixed-requires | "error" | 不容許混合requires文件 |
no-new-require | "error" | 不容許new require出現 |
no-path-concat | "error" | 不容許路徑以_連接 |
handle-callback-err | ["error", "err"] | 處理錯誤的回調函數 |