1)全局安装相关依赖包
$ npm -g install \babel-core@^6.22.1 \babel-eslint@^8.2.1 \eslint@^4.15.0 \eslint-config-standard@^10.2.1 \eslint-friendly-formatter@^3.0.0 \eslint-loader@^1.7.1 \eslint-plugin-import@^2.7.0 \eslint-plugin-node@^5.2.0 \eslint-plugin-promise@^3.4.0 \eslint-plugin-standard@^3.0.1 \eslint-plugin-vue@^4.0.0 \standard
2)设置全局配置文件
全局配置文件.eslintrc.js
我这边存放的目录是D:\WorkSpace\.eslintrc.js
相关报错信息已经写明了注释
module.exports = {'env': {'browser': true,'commonjs': true,'es6': true},'extends': ['eslint:recommended',// consider switching to `plugin:vue/strongly-recommended` or `plugin:vue/recommended` for stricter rules.'plugin:vue/essential',// https://github.com/standard/standard/blob/master/docs/RULES-en.md'standard'],'parserOptions': {'parser': 'babel-eslint','ecmaVersion': 6,'sourceType': 'module','ecmaFeatures': {'jsx': true}},'globals': {'native': true,'callNative': true,'process': true,'$': true,'encryptUtil': true,'CryptoJS': true,'wx': true,'getApp': true,'mpvuePlatform': true,'mpvue': true},'plugins': ['vue'],'rules': {// 强制使用一致的缩进'indent': ['error', 2, { 'SwitchCase': 1 ,'ArrayExpression': 'off'}],// 要求或禁止使用分号代替 ASI'semi': ['error', 'always'],// 警告 禁用 console , 仅允许使用 console.warn()和console.error()'no-console': ['warn', { 'allow': ['warn', 'error'] }],// 禁止条件表达式中出现赋值操作符'no-cond-assign': ['error', 'always'],// 禁止不必要的分号'no-extra-semi': 'error',// 禁止在return、throw、continue 和 break 语句之后出现不可达代码'no-unreachable': 'error',// 要求使用 isNaN() 检查 NaN'use-isnan': 'error',// 强制所有控制语句使用一致的括号风格'curly': 'error',// 警告 要求使用 === 和 !=='eqeqeq': 'warn',// 警告 强制尽可能地使用点号'dot-notation': 'warn',// 要求 switch 语句中有 default 分支'default-case': 'error',// 冒号左右的空格可以提高 case 和 default 子句的可读性。'switch-colon-spacing': ['error', {'after': true, 'before': false}],// 警告 alert、confirm 和 prompt'no-alert': 'warn',// 禁止出现空函数'no-empty-function': 'error',// 禁止多次声明同一变量'no-redeclare': 'error',// 禁止自身比较'no-self-compare': 'error',// 警告 要求使用 let 或 const 而不是 var,在使用 var 时'no-var': 'warn',// 禁止在变量定义之前使用它们'no-use-before-define': 'error',// 警告 未使用的变量'no-unused-vars': 'warn',// 警告 未在逗号周围使用空格'comma-spacing': 'warn',// 警告 在注释前没有空白'spaced-comment': ['warn', 'always'],// 警告 函数圆括号之前有一个空格'space-before-function-paren': 'off',// 警告 语句块之前的空格'space-before-blocks': 'warn',// 强制使用一致的反勾号、双引号或单引号'quotes': ['error', 'single', { 'allowTemplateLiterals': true ,'avoidEscape':true}],// 警告 关键字周围使用空格'keyword-spacing': 'warn',// 警告 函数中的变量分开声明'one-var': ['warn', {'initialized': 'consecutive'}],// 警告 文件末尾保留一行空行'eol-last': ['warn', 'always'],// iview 标签报错'vue/no-parsing-error': ['warn', { 'x-invalid-end-tag': false }],// 警告 禁止出现空函数'no-empty-function': 'warn',// 警告 禁用行尾空白'no-trailing-spaces': ['warn',{'ignoreComments': true}],// 警告 禁用稀疏数组,数组中有空元素'no-sparse-arrays': 'warn',// 强制操作符使用一致的换行符风格'operator-linebreak': ['error', 'before'],// 强制把object,array的props默认值转换为带有返回值的函数'vue/require-valid-default-prop': 'off'}}
3)设置VSCode配置文件
文件—》首选项—》设置—》{}(在这个图标在右上角)
完成后进入VSCode的配置文件settings.json
添加以下设置
以下设置依赖于VSCode的插件eslint、prettier、vetur
{"eslint.options": {"configFile": "D:/WorkSpace/.eslintrc.js"},// "eslint.autoFixOnSave": true,// 专门写Vue的eslint配置"eslint.validate": ["javascript","javascriptreact","html","vue",{"language": "html","autoFix": true},{"language": "vue","autoFix": true}],"files.associations": {"*.vue": "vue"},"editor.renderWhitespace": "all","editor.tabSize": 2,"typescript.format.insertSpaceBeforeFunctionParenthesis": true,"prettier.printWidth": 180,"prettier.singleQuote": true,"prettier.jsxSingleQuote": true,"prettier.trailingComma": "es5","vetur.format.defaultFormatter.js": "vscode-typescript","emmet.triggerExpansionOnTab": true,"vetur.format.defaultFormatter.html": "js-beautify-html",// 关闭VUE中HTML标签语法报错"vetur.validation.template": false,// vetur设置html默认格式化为js-beautify-html,如下设置可保证格式化vue文档时,template中的标签属性不换行"vetur.format.defaultFormatterOptions": {"js-beautify-html": {"wrap_line_length": 120,"end_with_newline": false,"wrap_attributes": "auto" // 是否进行属性强制换行[auto|force|force-aligned|force-expand-multiline]}},}
