VSCode Vue ESLint 格式化配置

本文介绍了如何在VSCode中配置Vetur和ESLint插件,以实现Vue项目的代码格式化。虽然ESLint可以很好地遵循.eslintrc.js的规则,但无法格式化.vue文件中的<style>标签样式。而Vetur格式化则不完全符合ESLint规则,需要通过配置Vetur格式化规则并结合ESLint检查来手动调整代码格式。

Vetur、ESLint插件安装这里就不多说了,直接看vscode settings.json配置:


重要:
使用ESLint格式化,能很好的符合.eslintrc.js文件里配置的rules,但有个很大的瑕疵,就是vue文件<style>标签里的样式无法进行格式化,只能独立编写到单独的样式文件中,再在<style>标签里@import 。
看到有个相关插件 eslint-plugin-vue-scpoed-css 但是貌似不太好用,可配置项也比较少,期待后面的完善。

  // ESLint formatter
  "eslint.format.enable": true,
  "editor.defaultFormatter": "dbaeumer.vscode-eslint", // 全局 formatter
  "[vue]": {
    "editor.defaultFormatter": "dbaeumer.vscode-eslint" // vue formatter
  },
  "[javascript]": {
    "editor.defaultFormatter": "dbaeumer.vscode-eslint" // js formatter
  },

使用Vetur格式化就无法自动符合.eslintrc.js里定义的规则了,只能配置Vetur的格式化规则,配合ESLint检查提示,手动调整到格式化的结果符合ESLint规则。

  // vue文件使用vetur格式化
  "[vue]": {
    "editor.defaultFormatter": "octref.vetur"
  },
  "vetur.format.defaultFormatter.html": "js-beautify-html", // vue文件中html模板使用js-beautify-html格式化
  "vetur.format.defaultFormatter.js": "vscode-typescript", // vue文件中js代码使用vscode-typescript格式化
  "vetur.format.options.tabSize": 4,
  "vetur.format.scriptInitialIndent": true, // <script>标签里的代码初始缩进
  "vetur.format.styleInitialIndent": true, // <style>标签里的样式初始缩进
  "vetur.format.defaultFormatterOptions": {
    // 参考: https://vuejs.github.io/vetur/guide/formatting.html#settings
    "js-beautify-html": {
      /**
       * 参考: https://github.com/beautify-web/js-beautify
       */
      "indent_size": 4, // 每下一个级别的缩进尺度,该值为 indent_char 的倍数
      "indent_char": " ", // 一个缩进尺度的填充内容
      "indent_with_tabs": false, // 以 tab 为缩进内容,该值为 true 时会覆盖 indent_char
      "wrap_attributes_indent_size": 8, // Indent wrapped attributes to after N characters - 属性换行缩进的级别数
      "eol": "\n", //
      "end_with_newline": true, //
      "indent_level": 0, //
      "preserve_newlines": true, // 保留空行
      "max_preserve_newlines": 10, // 最大保留空行数
      "space_in_paren": false, //
      "space_in_empty_paren": false, //
      "jslint_happy": false,
      "space_after_anon_function": false,
      "space_after_named_function": false,
      "brace_style": "collapse",
      "unindent_chained_methods": false,
      "break_chained_methods": false,
      "keep_array_indentation": false,
      "unescape_strings": false,
      "e4x": false,
      "comma_first": false,
      "operator_position": "before-newline",
      "indent_empty_lines": false,
      "templating": ["auto"],
      // "wrap_line_length": 0, // Maximum amount of characters per line (0 = disable)
      
      /**
       * 参考: https://github.com/vuejs/vetur/blob/master/server/src/modes/template/services/htmlFormat.ts
       */
      // "end_with_newline": false, // End output with newline
      // "indent_char": ' ', // Indentation character
      "indent_handlebars": false, // e.g. {{#foo}}, {{/foo}}
      "indent_inner_html": false, // Indent <head> and <body> sections
      "indent_scripts": "keep", // [keep|separate|normal]
      // "indent_size": 2, // Indentation size
      // "indent_with_tabs": false,
      // "max_preserve_newlines": 1, // Maximum number of line breaks to be preserved in one chunk (0 disables)
      // "preserve_newlines": true, // Whether existing line breaks before elements should be preserved
      "unformatted": [], // Tags that shouldn't be formatted. Causes mis-alignment
      "wrap_line_length": 0, // Lines should wrap at next opportunity after this number of characters (0 disables)
      /**
       * Wrap attributes - 属性换行:
       *    force:                  Wrap each attribute except first                            除了第一个属性外强制换行
       *    force-aligned:          Wrap each attribute except first and keep aligned           除了第一个属性外强制换行并且对齐
       *    force-expand-multiline: Wrap each attribute                                         强制展开成多行
       *    aligned-multiple:       Wrap when line length exceed, align attributes vertically   按 wrap_line_length 指定长度换行
       *    preserve:               Preserve wrapping of attributes                             未超长保留当前的换行不变,超长行按 wrap_line_length 换行
       *    preserve-aligned:       Preserve wrapping of attributes but align                   同 preserve,但是要齐
       *
       * 遗留问题:
       *    这里的aligned是与哪个对齐?
       */
      "wrap_attributes": "preserve", // Wrap attributes to new lines [auto|force|force-aligned|force-expand-multiline] ["auto"]
    },
  },
<think>嗯,用户想在VSCode配置ESLint实现自动格式化代码。我记得之前看过一些关于配置ESLint的文章,可能需要安装扩展,然后修改设置文件。首先,用户需要安装ESLint扩展,对吧?因为VSCode本身不带这个功能,必须通过扩展来实现。安装完扩展之后,可能需要在设置里启用自动修复和保存时格式化。 然后,用户需要确保项目里有ESLint配置文件,比如.eslintrc.js或者.eslintrc.json,否则可能不起作用。接着,配置VSCode的settings.json文件,这部分是关键。我记得有几个关键的配置项,比如editor.codeActionsOnSave,里面设置source.fixAll.eslint为true,这样保存的时候就会自动修复问题。另外,可能还需要设置eslint.format.enable为true,这样ESLint才能进行格式化。 用户提到的引用里提到了一些配置项,比如eslint.workingDirectories,这个可能是当项目有多个工作目录时需要设置的。比如前后端分开的项目,可能需要指定不同的目录。还有eslint.validate,指定需要检查的文件类型,比如javascript、vue、html等。这些都需要根据用户的实际情况来调整。 还有可能遇到的问题是,用户已经安装了其他格式化工具,比如Prettier,这时候可能会有冲突。需要确保ESLint格式化优先,或者在设置中禁用其他格式化工具。另外,安装依赖项也很重要,如果项目里没有安装eslint包,或者相关的插件,比如eslint-plugin-vue,那么即使配置正确,ESLint也无法正常工作。 测试的时候,用户可能需要重启VSCode,或者重新加载窗口,让配置生效。然后随便写点有问题的代码,保存看看是否自动修复了。比如声明一个变量但未使用,或者缩进不正确,保存后应该自动修正。 可能用户还不太清楚如何打开settings.json,需要指导他们通过命令面板或者界面进入设置,然后编辑JSON文件。另外,全局安装和项目内安装ESLint的区别也很重要,如果项目有自己的配置,应该优先使用项目内的。 总结一下步骤应该是:安装扩展,确保ESLint配置存在,修改VSCode设置,处理可能的冲突,安装依赖,测试。然后常见问题可能包括配置未生效、与其他工具冲突、缺少依赖等,需要逐一排查。</think>以下是在VSCode配置ESLint实现自动格式化的分步指南: ### 一、基础配置步骤 1. **安装ESLint扩展** - 打开VSCode扩展商店(快捷键`Ctrl+Shift+X`) - 搜索并安装"ESLint"(作者为Microsoft) 2. **创建ESLint配置文件** ```bash # 在项目根目录执行(需已安装Node.js) npx eslint --init ``` 根据提示选择配置风格(推荐选择"Popular style guide"->"Standard") 3. **配置settings.json** 打开VSCode设置文件(`Ctrl+,` -> 右上角JSON文件图标): ```json { "editor.codeActionsOnSave": { "source.fixAll.eslint": true }, "eslint.format.enable": true, "editor.formatOnSave": false // 关闭默认格式化防止冲突[^2] } ``` ### 二、进阶配置(多项目场景) ```json { "eslint.workingDirectories": ["./front", "./back"], // 多项目路径 "eslint.validate": [ "javascript", "typescript", "vue", "html" ], "editor.tabSize": 2 // 与ESLint规则保持一致[^1] } ``` ### 三、常见问题解决 1. **规则不生效检查清单**: - 确保项目根目录有`.eslintrc.*`配置文件 - 检查是否安装对应依赖(如`eslint-plugin-vue`) - 查看VSCode右下角ESLint状态是否显示为生效 2. **与Prettier的兼容配置**: ```bash npm install --save-dev eslint-config-prettier eslint-plugin-prettier ``` 修改`.eslintrc.js`: ```js module.exports = { extends: [ 'plugin:prettier/recommended' // 必须放在最后 ] } ``` ### 四、验证配置 创建测试文件`test.js`: ```javascript const a=1 // 故意写错格式 ``` 保存文件后应自动修正为: ```javascript const a = 1 ``` ###
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值