vscode工作區設置:
{ "editor.tabSize": 2, // 開啟保存后即觸發格式化 不開開了會根據"vetur.format.defaultFormatter.js": "vscode-typescript", // "editor.formatOnSave": true, //關閉右側快速預覽 "editor.minimap.enabled": true, "editor.fontSize": 14, //設置文字大小 "editor.lineHeight": 0, //設置文字行高 "editor.lineNumbers": "on", //開啟行數提示 "editor.quickSuggestions": { //開啟自動顯示建議 "other": true, "comments": true, "strings": true }, "window.zoomLevel": 0, // 調整窗口的縮放級別 // 文件自動保存 // "files.autoSave": "afterDelay", // "files.autoSaveDelay": 30000, // eslint.autoFixOnSave - enables auto fix on save. // Please note auto fix on save is only available if VS Code's files.autoSave is either off, onFocusChange or onWindowChange // . It will not work with afterDelay. //https://marketplace.visualstudio.com/items?itemName=dbaeumer.vscode-eslint // #每次保存的時候將代碼按eslint格式進行修復 // "eslint.autoFixOnSave": true,廢除 // 用該屬性觸發eslint修復 "editor.codeActionsOnSave": { "source.fixAll.eslint": true }, //根據文件后綴名定義vue文件類型 "files.associations": { "*.vue": "vue" }, // 添加 vue 支持 "eslint.validate": [ "javascript", "javascriptreact", { "language": "vue", "autoFix": true } ], // 該屬性相當於配置js文件的操作 // "[javascript]": { // "editor.formatOnSave": true, // "editor.defaultFormatter": "esbenp.prettier-vscode" // }, // #讓prettier使用eslint的代碼格式進行校驗,prettier默認覆蓋vscode格式化快捷鍵 // 該屬性已廢除 // "prettier.eslintIntegration": true, // #聲明結尾使用分號(默認true) "prettier.semi": false, // #使用帶引號替代雙引號 "prettier.singleQuote": true, // #只有一個參數的箭頭函數的參數是否帶圓括號(默認avoid) "prettier.arrowParens": "avoid", // #多行JSX中的>放置在最后一行的結尾,而不是另起一行(默認false) "prettier.jsxBracketSameLine": false, //關閉rg.exe進程 用cnpm導致會出現rg.exe占用內存很高 "search.followSymlinks": false, // 默認格式化vue中的<script> region的方式,按編輯器自帶的ts格式進行格式化 "vetur.format.defaultFormatter.js": "vscode-typescript", // 默認格式化vue中的<template> region的方式 //由於prettier不能格式化vue文件template 所以使用js-beautify-html格式化 // https://vuejs.github.io/vetur/formatting.html#formatters "vetur.format.defaultFormatter.html": "js-beautify-html", // 給js-beautify-html設置屬性隔斷 "vetur.format.defaultFormatterOptions": { "js-beautify-html": { "wrap_attributes": "force-aligned", // Maximum number of line breaks to be preserved in one chunk (0 disables) "max_preserve_newlines": 0 } }, // js默認偏移一個indent "vetur.format.scriptInitialIndent": true, // style默認偏移一個indent "vetur.format.styleInitialIndent": true, // 函數名后增加空格 "javascript.format.insertSpaceBeforeFunctionParenthesis": true, "javascript.format.insertSpaceAfterFunctionKeywordForAnonymousFunctions": true, // TS "typescript.format.insertSpaceBeforeFunctionParenthesis": true }
需要安裝vscode插件 vetur eslint prettier
1.首先要讓eslint識別vue文件 安裝插件eslint-plugin-vue(識別react eslint-plugin-react)
然后在eslintrc里添加插件
並且在工作區內配置
2.讓每次保存都格式化
"editor.formatOnSave": true,
3.每次保存都按eslint去修復
"eslint.autoFixOnSave": true,
4.但是由於prettier還不識別vue,可以通過設置
// #讓prettier使用eslint的代碼格式進行校驗,prettier默認覆蓋vscode格式化快捷鍵
"prettier.eslintIntegration": true,
5.再通過vetur的格式化,vetur的默認代碼風格化使用的就是prettier,會自動檢索項目根目錄下的prettier配置文件進行格式化,只有template用js-beautify-html

參考:
eslint規則配置詳解:https://segmentfault.com/a/1190000008742240
Airbnb JavaScript 編碼規范 https://github.com/yuche/javascript
vutur+eslint+prettier :https://segmentfault.com/a/1190000014785115?utm_source=index-hottest
https://www.jb51.net/article/135191.htm
https://blog.csdn.net/ljt123456765/article/details/81356792
eslint+prettier格式化代碼(非vetur,通用版本):https://www.jb51.net/article/145970.htm