VSCode supports ESLint automatic formatting
Install plugin
1. Install ESLint
2. Install Vetur
3.安装Prettier - Code formatter
The above 3 plugins can be installed directly by searching in the app store
Configure automatic formatting
1. Click on the upper left corner [File]-[Preferences]-[Settings]
2. Click the icon in the red box in the upper right corner to open the settings.json file
3. Enter the following
{
"editor.lineNumbers": "on", //开启行数提示
"editor.quickSuggestions": {
//开启自动显示建议
"other": true,
"comments": true,
"strings": true
},
"prettier.useTabs": true, //使用制表符缩进
"editor.tabSize": 2, //制表符符号eslint
"editor.formatOnSave": true, //每次保存自动格式化
"prettier.semi": true, //去掉代码结尾的分号
"prettier.singleQuote": true, //使用单引号替代双引号
"prettier.trailingComma": "none", //去除对象最末尾元素跟随的逗号
"javascript.format.insertSpaceBeforeFunctionParenthesis": true, //让函数(名)和后面的括号之间加个空格
"vetur.format.defaultFormatter.html": "js-beautify-html", //格式化.vue中html
"vetur.format.defaultFormatter.js": "vscode-typescript", //让vue中的js按编辑器自带的ts格式进行格式化
"[vue]": {
"editor.defaultFormatter": "esbenp.prettier-vscode"
},
"[javascript]": {
"editor.defaultFormatter": "esbenp.prettier-vscode"
},
"eslint.run": "onSave",
"editor.codeActionsOnSave": {
"source.fixAll.eslint": true
},
"prettier.printWidth": 200, //指定代码长度,超出换行
"prettier.requireConfig": true, //需要prettier.requireConfig格式化
"prettier.useEditorConfig": false,
// "eslint.validate": [
// //开启对.vue文件中错误的检查
// "javascript",
// "javascriptreact",
// {
// "language": "html",
// "autoFix": true
// },
// {
// "language": "vue",
// "autoFix": true
// }
// ],
"terminal.integrated.rendererType": "dom",
"[typescript]": {
"editor.defaultFormatter": "esbenp.prettier-vscode"
}
}
Concluding remarks
When saving the file by CTRL+S, you can use the ESLint format to call the file
Support the new version of VSCode (2020 version), a large number of online tutorials are relatively old and cannot achieve perfect formatting