在vue-cli中为了能让vscode能提示.vue文件中的js代码,咱们引入了eslint-plugin-html这个eslint插件(使用方法参考VSCode环境下配置ESLint 对Vue单文件的检测)
最近开始使用vue-cli3构建项目,主要目的是为了简化项目代码结构和提升编译性能。当咱们使用之前的方案去实现vscode对.vue文件的eslint检测时却发现始终没法识别,并且提示如下内容javascript
提示信息很容易理解,eslint没有把当前文件当作vue文件处理,而是当作了普通的js文件处理,固然,js文件的外层代码确定不能含有html标记。最后,咱们找到了eslint-plugin-vue,这个插件能完美处理.vue文件,并且还预置了不少可复用的rules(eslint规则)。使用方法以下:html
注:vue-cli3默认不会在根目录建立.eslintrc.js文件,由于vue-cli3除了这种方法配置eslint之外还能够在package.json中经过eslintConfig属性去配置,可是这种方式须要严格遵照json语法规则,咱们建议若是您的eslint配置较为复杂,仍是在根目录本身建立一个.eslintrc.js文件,这样就能够按照js语法规则去写配置项,也方便注释
module.exports = { // ...其余配置项 plugins: [ 'vue' ] // ...其余配置项 }
增长一个文件检测说明配置extends: [ module.exports = { root: true, // https://github.com/standard/standard/blob/master/docs/RULES-en.md extends: [ 'plugin:vue/base' ], }
这里咱们使用的是base里面的规则,更多的预置规则能够参考文档(eslint-plugin-vue Available rules)[https://eslint.vuejs.org/rules/]vue
关于eslint规则复用能够参考文档https://cn.eslint.org/docs/de...java
module.exports = { root: true, parserOptions: { parser: 'babel-eslint', sourceType: 'module' } // ...其余配置项 }
"eslint.validate": [ "javascript", "javascriptreact", { "language": "vue", "autoFix": true } ]
附完整的.eslintrc.js文件react
// https://eslint.org/docs/user-guide/configuring module.exports = { root: true, parserOptions: { parser: 'babel-eslint', sourceType: 'module' }, env: { browser: true }, // https://github.com/standard/standard/blob/master/docs/RULES-en.md extends: [ 'plugin:vue/base' ], // required to lint *.vue files plugins: [ 'vue' ], // add your custom rules here 'rules': { // allow paren-less arrow functions 'indent': [2, 2], // 两个空格的缩进 'quotes': [2, 'single'], // js必须使用单引号 'linebreak-style': [2, 'unix'], // 换行风格 unix/windows 'semi': [2, 'always'], // 语句强制分号结尾 'no-console': [1], // 不容许console语句 'no-unused-vars': [1], // 声明了变量可是没有使用检测 'space-unary-ops': [1, { 'words': true, 'nonwords': false }], // 一元运算符的前/后要不要加空格 'brace-style': [2, '1tbs', { 'allowSingleLine': false }], // 大括号风格 'comma-spacing': [2, { 'before': false, 'after': true }], // 逗号后有空格,前没有空格 'comma-style': [2, 'last'], // 逗号跟在结尾 'key-spacing': [2, { 'beforeColon': false, 'afterColon': true }], // 对象字面量中冒号的先后空格 'lines-around-comment': [ // 行前/行后备注 2, { 'beforeBlockComment': false, // 段注释的先后 'beforeLineComment': false, // 行注释的前面 'afterBlockComment': false, // 块注释的后面 'afterLineComment': false, // 行注释的后面 'allowBlockStart': true, 'allowObjectStart': true, 'allowArrayStart': true }], 'max-depth': [2, 4], // 代码最多容许4层嵌套 'max-len': [1, 160, 2], 'max-nested-callbacks': [2, 3], // 回调嵌套深度 'max-params': [2, 5], // 函数最多只能有5个参数 'max-statements': [1, 80], // 单个函数最多80条语句 'no-array-constructor': [2], // 禁止使用数组构造器 'no-lonely-if': 2, // // 禁止else语句内只有if语句 'no-multiple-empty-lines': [2, { 'max': 3, 'maxEOF': 1 }], // 空行最多不能超过2行 'no-nested-ternary': 2, // 不使用嵌套的三元表达式 'no-spaced-func': 2, // 函数调用时 函数名与()之间不能有空格 'no-trailing-spaces': 2, // 一行结束后面不要有空格 'no-unneeded-ternary': 2, // 禁止没必要要的嵌套 var isYes = answer === 1 ? true : false;简单的判断用三元表达式代替 'object-curly-spacing': [2, 'always', { // 大括号内是否容许没必要要的空格 always始终容许;never始终不容许 'objectsInObjects': false, 'arraysInObjects': false }], 'arrow-spacing': 2, // =>的前/后括号 'block-scoped-var': 2, // 块语句中使用var 'no-dupe-class-members': 2, // 'no-var': 1, // 禁用var,用let和const代替 'object-shorthand': [1, 'always'], // 强制对象字面量缩写语法 'array-bracket-spacing': [2, 'never'], // 是否容许非空数组里面有多余的空格 'operator-linebreak': [2, 'after'], // 换行时运算符在行尾仍是行首 'semi-spacing': [2, { 'before': false, 'after': true }], // 分号先后空格 'keyword-spacing': ['error'], 'space-before-blocks': 2, // 不以新行开始的块{前面要不要有空格 'block-spacing': [2, 'always'], 'space-before-function-paren': [2, 'never'], // 函数定义时括号前面要不要有空格 'space-in-parens': [2, 'never'], // 小括号里面要不要有空格 'spaced-comment': [1, 'always', { 'exceptions': ['-', '*', '+'] }], // 注释风格要不要有空格什么的 'arrow-parens': 0, // allow async-await 'generator-star-spacing': 0, // allow debugger during development 'no-debugger': process.env.NODE_ENV === 'production' ? 2 : 0 }, globals: { '$': false, 'jquery': false, 'ActiveXObject': false, 'arbor': true, 'layer': false } };