VSCode在开发Vue项目的时候,尽管我们安装了格式化插件,但是最终运行时还是会有一堆的警告,就是因为格式化代码的方式和eslint的规范不一样导致的。
在VSCode的文件
-首选项
-设置
里,添加如下代码即可,从此直接 Ctrl+S 就能一键格式化了
代码如下:
{
// vscode默认启用了根据文件类型自动设置tabsize的选项
"editor.detectIndentation": false,
// 重新设定tabsize
"editor.tabSize": 2,
// 每次保存的时候自动格式化
"editor.formatOnSave": true,
// 每次保存的时候将代码按eslint格式进行修复
"editor.codeActionsOnSave": {
"source.fixAll.eslint": true
},
// 保留行末的分号
"prettier.semi": true,
// 使用双引号
"prettier.singleQuote": false,
// 不保留对象最后元素后面的逗号
"prettier.trailingComma": "none",
// 让函数(名)和后面的括号之间加个空格
"javascript.format.insertSpaceBeforeFunctionParenthesis": true,
"[vue]": {
"editor.defaultFormatter": "esbenp.prettier-vscode"
}
}