如何根据项目的eslint去配置vscode的setting

一、安装 必要的插件

1-1 Eslint

在这里插入图片描述

1-2 Prettier-Code formatter

在这里插入图片描述

目前新的vscode 都需要安装这个 代码格式化插件

  • 否则会提示 Extension ‘esbenp.prettier-vscode’ is configured as formatter but not available. Select a different default formatter to continue.

1-3 安装Vetur

格式.vue的文件

二、配置相关文件

2-1 配置 setting.json

2-1-1 找到setting.json文件配置vscode
  1. 找到File > Preference > Settings > Text Editor > Code Actions On Save > Edit in setting.json打开进行编辑

在这里插入图片描述

在这里插入图片描述

2-1-2 在文件中添加如下配置

添加如下的配置到你的setting.json (这个注释很全就不过得解释了)

{
  "workbench.startupEditor": "newUntitledFile", // window be show of level in ide
  "git.ignoreMissingGitWarning": true,
  "explorer.confirmDelete": false,
  "workbench.colorTheme": "Monokai",
  "workbench.colorCustomizations": {
      "[Monokai]": {
      "editor.background": "#1a2c1c",
      "sideBar.background": "#2a3b2d",
      "activityBar.background": "#7154978a",
      "icon.foreground": "#893ecf",
      "activityBar.inactiveForeground": "#ffee00b4",
      },
  },
  "docthis.authorName": "tomAnny",
  "docthis.includeDescriptionTag": true,
  "docthis.includeDateTag": true,
  "docthis.includeAuthorTag": true,
  "vsicons.dontShowNewVersionMessage": true,
  "terminal.integrated.rendererType": "dom",
  // vscode默认启用了根据文件类型自动设置tabsize的选项
  "editor.detectIndentation": false,
  // 重新设定tabsize
  "editor.tabSize": 2,
  // #每次保存的时候自动格式化 
  "editor.formatOnSave": true,
  "editor.formatOnType": true,
  // 强制单引号
  "prettier.singleQuote": true,
  "prettier.semi": false,
  // 尽可能控制尾随逗号的打印
  "prettier.trailingComma": "all",
  // #这个按用户自身习惯选择-- prettier 或者js-beautify-html
  "vetur.format.defaultFormatter.html": "js-beautify-html",
  // #让vue中的js按编辑器自带的ts格式进行格式化 
  "vetur.format.defaultFormatter.js": "vscode-typescript",
  "html.format.indentHandlebars": true,
  "javascript.preferences.quoteStyle": "single",
  "typescript.preferences.quoteStyle": "single",
  "html.format.enable": false,
  "html.format.preserveNewLines": false,
  "diffEditor.ignoreTrimWhitespace": false,
  "vetur.format.defaultFormatterOptions": {
    "js-beautify-html": {
      "wrap_attributes": "force-aligned"
      // #vue组件中html代码格式化样式
    }
  },
  //防止VSCode启动带有node_modules的项目的时候很卡的问题
  "search.followSymlinks": false,
  "files.autoSave": "onWindowChange",
  "[vue]": {
    //"editor.defaultFormatter": "octref.vetur"
    "editor.defaultFormatter": "esbenp.prettier-vscode"
  },
  "[javascript]": {
    "editor.defaultFormatter": "HookyQR.beautify"
  },
  "[scss]": {
    "editor.defaultFormatter": "HookyQR.beautify"
  },
  "[html]": {
    "editor.defaultFormatter": "HookyQR.beautify"
  },
  "[css]": {
    "editor.defaultFormatter": "HookyQR.beautify"
 },
  // #每次保存的时候将代码按eslint格式进行修复
  "editor.codeActionsOnSave": {
    "source.fixAll.eslint": true
  },
  "window.zoomLevel": -2,
}

具体配置可以参考官网 对vscode进行setting配置

  • 第二个配置


{
    "eslint.options": {
      "extensions": [
        ".js",
        ".vue"
      ]
    },
    "eslint.validate": [
      "javascript",
      {
        "language": "vue",
        "autoFix": true
      },
      "html",
      "vue"
    ],
    "eslint.autoFixOnSave": true,
    "editor.codeActionsOnSave": {
      "source.fixAll.eslint": true
    },
    "eslint.codeAction.disableRuleComment": {},
    "eslint.alwaysShowStatus": true,
    "window.zoomLevel": -2,
  }

2-2 配置 .eslintrc.js

// https://eslint.org/docs/user-guide/configuring

module.exports = {
  root: true,
  parserOptions: {
    parser: 'babel-eslint'
  },
  env: {
    browser: true
  },
  extends: [
    // https://github.com/vuejs/eslint-plugin-vue#priority-a-essential-error-prevention
    // 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'

  ],
  // required to lint *.vue files
  plugins: [
    'vue'
  ],
  // add your custom rules here
  rules: {
    // allow async-await
    'generator-star-spacing': 'off',
    // allow debugger during development
    'no-debugger': process.env.NODE_ENV === 'production' ? 'error' : 'off'
  }
}

2-3 配置 .editorconfig

  • 再配置一个代码 空格,缩进、换行等规范
  • 看看官方对这个editor的介绍
    • What is EditorConfig?
      EditorConfig helps maintain consistent coding styles for multiple developers working on the same project across various editors and IDEs. The EditorConfig project consists of a file format for defining coding styles and a collection of text editor plugins that enable editors to read the file format and adhere to defined styles. EditorConfig files are easily readable and they work nicely with version control systems.
root = true

[*]
charset = utf-8
indent_style = space
indent_size = 2
end_of_line = lf
insert_final_newline = true
trim_trailing_whitespace = true

2-4 配置.eslintignore

对以下文件不进行eslint校验

/build/
/config/
/dist/
/*.js

诶,和规则不一样,多敲几个空格,好了。报错,Ctrl+s之后自动修改
嘿!!好了 ,哈哈

三、之前配置记录的小小问题【各位不用看了,仅为xiuxiu记录噢~~】

3-1

在这里插入图片描述
在这里插入图片描述

查阅过的文章付下:

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值