配置 Eslint

306 阅读1分钟

eslint-plugin-vue

使用vue-cli创建时自动会带上这个

官网

eslint.vuejs.org/

standard

这是一份适应较广的标准

  1. 在创建项目时选择它
Please pick a preset: Manually select features
? Check the features needed for your project: Choose Vue version, Babel, Linter
? Choose a version of Vue.js that you want to start the project with 3.x (Preview)
? Pick a linter / formatter config:
  ESLint with error prevention only
  ESLint + Airbnb config
> ESLint + Standard config
  ESLint + Prettier

官网

standardjs.com/

prettierrc

在创建项目时,对于eslint很多都是采用默认的第一个选项然后再进行配置,prettier也是其中的一种标准,我选择使用它,现在开始进行配置。

  1. 在创建项目时选择它
Please pick a preset: Manually select features
? Check the features needed for your project: Choose Vue version, Babel, Linter
? Choose a version of Vue.js that you want to start the project with 3.x (Preview)
? Pick a linter / formatter config:
  ESLint with error prevention only
  ESLint + Airbnb config
  ESLint + Standard config
> ESLint + Prettier
  1. 在根目录创建文件.prettierrc
{
  "semi": false,
  "singleQuote": true,
  "printWidth": 80
}

这里可以进行prettier配置

官网

prettier.io/

自动格式化

  1. vscode中安装Eslint插件

  2. 在项目根目录创建.vscode文件夹,然在其中创建settings.json

{
  "editor.codeActionsOnSave": {
    "source.fixAll": true
  }
}

这样就可以在保存时,自动格式化代码了