Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

VS Code使用之Vue工程配置ESLint #4

Open
way2ex opened this issue Dec 28, 2017 · 0 comments
Open

VS Code使用之Vue工程配置ESLint #4

way2ex opened this issue Dec 28, 2017 · 0 comments
Labels

Comments

@way2ex
Copy link
Owner

way2ex commented Dec 28, 2017

首先确保VS Code 安装了 VeturEslint 插件。
然后使用了vue-cli提供的简单webpack模板,这样创建的工程默认没有添加ESLint。

vue init webpack-simple test-vscode

配置过程

  1. 全局安装最新的eslint
npm i -g eslint@latest

运行

eslint --init

eslint-init

会安装以下依赖

eslint-config-standard@latest
eslint-plugin-import@>=2.2.0
eslint-plugin-node@>=5.2.1
eslint-plugin-promise@>=3.5.0
eslint-plugin-standard@>=3.0.0

同时在项目目录下生成.eslintrc.js文件。里面只有最基本的内容:

module.exports = {
    "extends": "standard"
};

表明我们使用的规则是standard规范所定义的规则。
2. 然后本地安装最新的eslint

npm i -D eslint@latest
  1. package.jsonscripts中添加一行:
 "lint": "eslint --ext .js,.vue src"

运行:

npm run lint

VS Code会提示我们找不到eslint-config-standard
报错提示

安装它:

npm i -D eslint-config-standard

然后运行

npm run lint

这时就会有报错的提示了。
lint_err
code_err

  1. 但是在.vue文件中出错的地方并没有相应的提示。这时Vetur排上用场了。 在VS Code的设置里面添加如下规则:
{
"eslint.validate": [
        "javascript",
        "javascriptreact",
       {  
            "language": "vue",
             "autoFix": true
        }
    ]
}

注意这里添加了 .vue 文件自动修复的规则,如果不添加这个规则,则保存时不会自动修复 .vue文件。
这样就添加了对.vue文件的支持。
5. 但是这样会对.vue文件中的标签报解析错误
parser_error

这时需要安装eslint-plugin-vue@next插件。

 npm install -D eslint-plugin-vue@next

同时在.eslintrc.js中添加使用vue插件的扩展。

// .eslintrc.js
module.exports = {
    "extends": [
           "standard",
           "plugin:vue/base"
      ]
}

这样,就可以对.vue文件提供实时检查的功能了。

  1. 对于多余的逗号这种错误,可以在保存的时候让eslint插件自动修复。 更改VS Code中的设置,添加如下规则:
{
  "eslint.autoFixOnSave": true
}

总结

通过使用VS Code的插件 VeturESLint来对Vue工程中的.vue提供代码检查的功能。

  • 需要安装的依赖:
     "eslint": "^4.14.0",
    "eslint-config-standard": "^11.0.0-beta.0",
    "eslint-plugin-import": "^2.8.0",
    "eslint-plugin-node": "^5.2.1",
    "eslint-plugin-promise": "^3.6.0",
    "eslint-plugin-standard": "^3.0.1",
    "eslint-plugin-vue": "^4.0.0-beta.4",
    注意: 这里 ESLinteslint-plugin-vue需要是最新的。
  • .eslintrc的配置
     module.exports = {
     "extends": [
         "standard",
         "plugin:vue/base"
     ]
     };
  • VS Code 的配置
      {
       "eslint.validate": [
        "javascript",
        "javascriptreact",
        {
            "language": "vue",
             "autoFix": true
        }
    ],
    "eslint.autoFixOnSave": true
      }

参考资料:
Vetur文档
ESLint文档
eslint-plugin-vue

@way2ex way2ex changed the title VS Code使用之Vue工程配置eslint VS Code使用之Vue工程配置ESLint Dec 28, 2017
@way2ex way2ex added the VS Code label Dec 29, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

1 participant