在用VScode中寫vue項目build時,經常不能通過代碼檢查, 多數原因是之前的編碼習慣引起的, 比如引號, 空格, 換行,分號等。
用好VScode的格式化功能對此可以有很大的幫助。
但發現VScode自動格式化時不會自動去掉多餘分號, 網上搜索出來的試了都不行。
還是自己仔細研究了一下設置選項, 給搞好了.
以下是我最後的配置方法:
一、安裝插件:
Vetur, ESLint,Prettier
二、修改配置
{
//====== 通用選項 ======
"terminal.integrated.shell.windows": "C:\\WINDOWS\\System32\\cmd.exe",
"npm.packageManager": "npm",
"workbench.sideBar.location": "left",
"workbench.activityBar.visible": true,
"workbench.statusBar.visible": true,
"editor.minimap.enabled": false,
"workbench.iconTheme": "vscode-icons",
"editor.renderLineHighlight": "all",
"editor.renderWhitespace": "selection",
"editor.mouseWheelZoom": true,
"editor.cursorWidth": 3,
//"workbench.colorTheme": "Solarized Dark" //暗陰
//"workbench.colorTheme": "Monokai Dimmed" //暗暖
//"workbench.colorTheme": "Monokai" //暗涼
//"workbench.colorTheme": "Visual Studio Light" //亮
//====== vscode自帶格式化功能配置 ======
"editor.formatOnSave": true,
"editor.formatOnType": true,
"editor.formatOnPaste": true,
"editor.detectIndentation": false, //關閉檢測第一個tab後面就tab
"editor.renderControlCharacters": true, //製表符顯示->
"editor.insertSpaces": true, //轉爲空格
"editor.tabSize": 2, //tab爲四個空格
"javascript.format.semicolons": "remove",
"typescript.format.semicolons": "remove",
"javascript.format.insertSpaceBeforeFunctionParenthesis": true, //函數名與()間加空隔
"javascript.preferences.quoteStyle": "single",
"typescript.preferences.quoteStyle": "single",
"javascript.format.enable": true, //自帶默認javascript格式化
"typescript.format.enable": true, //自帶默認typescript格式化
"json.format.enable": true, //自帶默認json格式化
"html.format.indentInnerHtml": false, //自帶默認html格式化
//====== prettier格式化,能使每一種語言默認格式化規則 ======
"editor.defaultFormatter": "esbenp.prettier-vscode",
"prettier.printWidth": 100, // 超過最大值換行
"prettier.tabWidth": 2, // 縮進字節數
"prettier.useTabs": false, // 縮進不使用tab,使用空格
"prettier.semi": false, // 句尾分號
"prettier.singleQuote": true, // 使用單引號代替雙引號
"prettier.arrowParens": "avoid", // (x) => {} 箭頭函數參數只有一個時是否要有小括號。avoid:省略括號
"prettier.bracketSpacing": true, // 在對象,數組括號與文字之間加空格 "{ foo: bar }"
"prettier.jsxSingleQuote": true, // 在jsx中使用單引號代替雙引號
//"prettier.trailingComma": "es5", // 在對象或數組最後一個元素後面是否加逗號(在ES5中加尾逗號)
// --- 部分文件格式化在後面單獨設置 ---
"prettier.disableLanguages": [
"vue",
"typescript",
"javascript",
"jsonc"
],
//====== 單獨設置文件格式化 ======
"[jsonc]": {
"editor.defaultFormatter": "vscode.json-language-features"
},
"[javascript]": {
"editor.defaultFormatter": "vscode.typescript-language-features"
},
"[typescript]": {
"editor.defaultFormatter": "vscode.typescript-language-features"
},
// ------ 用vetur格式化vue文件配置 ------
"[vue]": {
"editor.defaultFormatter": "octref.vetur"
},
"vetur.format.defaultFormatter.html": "js-beautify-html",
"vetur.format.defaultFormatter.css": "prettier",
"vetur.format.defaultFormatter.postcss": "prettier",
"vetur.format.defaultFormatter.scss": "prettier",
"vetur.format.defaultFormatter.less": "prettier",
"vetur.format.defaultFormatter.stylus": "stylus-supremacy",
//"vetur.format.defaultFormatter.js": "prettier", //解決不了 函數名與()間需要加空隔的需求
//"vetur.format.defaultFormatter.ts": "prettier", //同上
"vetur.format.defaultFormatter.js": "vscode-typescript", //解決不了 雙引號需要自動轉單引號的需求, 不過通過eslint插件保存時自動修復
"vetur.format.defaultFormatter.ts": "vscode-typescript", //同上
"vetur.format.defaultFormatterOptions": {
"js-beautify-html": {
//"wrap_attributes": "force-aligned",
"wrap_attributes": "force-expand-multiline"
},
"prettyhtml": {
"singleQuote": false,
"sortAttributes": false,
},
"prettier": {
"printWidth": 200,
"singleQuote": true, //使用單引號替換雙引號
"semi": false //結尾不加分號
},
/* "vscode-typescript": {
"singleQuote": true, //使用單引號替換雙引號,但配置後好像還是沒有反應
"semi": false //結尾不加分號
} */
},
// ====== eslint 保存時自動修復格式配置 ======
"editor.codeActionsOnSave": {
"source.fixAll.eslint": true, // For ESLint
"source.fixAll.tslint": true, // For TSLint
"source.fixAll.stylelint": true // For Stylelint
},
"eslint.validate": [
"javascript",
"javascriptreact",
"typescript",
"typescriptreact",
"html",
"vue",
"less",
"scss",
],
"eslint.format.enable": true,
}
三、配置要點
大多數用prettier格式化, vue用vetur格式化, js部份及js文件用vscode自帶的格式化, 並用ESLint加上保存時自動修復。
這樣寫完代碼保存後,再執行build時問題應該就會少很多了.