为了实现使用 VSCode 开发 Vue 项目时,使用ESLint,并在保存文件的时候自动解决格式问题,步骤如下:
如果项目中还没有安装 ESLint,则先按照 Vue 的文档添加 ESLint 插件。
然后 在 VSCode 中,
- 设置>扩展> ESLint
-
选中 ESLint: Auto Fix On Save
image.png -
修改 ESLint: Run,把 onType 改成 onSave。不然打字的时候就会有黄色的波浪线警告,没必要。
image.png - 点击“在 settings.json 中编辑“”,添加:
"eslint.validate": [
"javascript",
"javascriptreact",
{
"language": "html",
"autoFix": true
},
{
"language": "vue",
"autoFix": true
}
]
其实第 2、3 步相当于在 settings.json 中添加:
"eslint.autoFixOnSave": true,
"eslint.run": "onSave",