一. VSCode安裝插件
ESLint
Prettier
Vetur
二. 工程裡建立檔案.eslintrc.js
/*
* @Author: your name
* @Date: 2021-03-30 22:10:31
* @LastEditTime: 2021-04-17 22:11:10
* @LastEditors: Please set LastEditors
* @Description: In User Settings Edit
* @FilePath: \web_page\.eslintrc.js
*/
module.exports = {
root: true,
parserOptions: {
parser: 'babel-eslint',
sourceType: 'module'
},
// 配置的環境
env: {
browser: true,
node: true,
es6: true,
},
extends: ['plugin:vue/recommended', 'eslint:recommended'],
rules: {
// 開啟 console
'no-console': 'off',
// 禁止不必要的 catch 子句
'no-useless-catch': 'off',
// 禁止使用異步函數作為 Promise executor
'no-async-promise-executor': 'off',
// 可以出現未使用過的變量
'no-unused-vars': 'off',
// vue項目中,判斷是否為生産環境,如果是的話,就提醒,否則話可以使用debugger
"no-debugger": process.env.NODE_ENV === "production" ? "warn" : "off",
// tab縮進為4空格
"indent": ["off", 4],
// 關閉下列提醒
"vue/html-self-closing": "off",
"vue/html-closing-bracket-newline": "off",
"vue/max-attributes-per-line": "off",
"vue/html-indent": "off",
}
}
三. 打開VSCode設定,配置settings.json
先按步驟打開 setting 界面,
Code --> preferences -->setting (也可以快捷鍵 command + ,(mac) 直接打開)現在看到的是界面配置模式,點選右上角的紅色區域按鈕(如下圖),可以打開 settings.json 檔案。
附錄1:我的VSCode配置(2020-08)
儲存時自動修複
prettier使用eslint格式化
vetur使用prettier格式化
{
// vscode預設啟用了根據檔案類型自動設定tabsize的選項
"editor.detectIndentation": false,
// 重新設定tabsize
"editor.tabSize": 4,
// #每次儲存的時候自動格式化
"editor.formatOnSave": true,
// eslint配置項,儲存時自動修複錯誤
"editor.codeActionsOnSave": {
"source.fixAll.eslint": true
},
// 添加 vue 支援
"eslint.validate": [
"javascript",
"javascriptreact",
"html",
"vue"
],
// #讓函數(名)和後面的括号之間加個空格
"javascript.format.insertSpaceBeforeFunctionParenthesis": true,
// 圖示樣式(需要安裝插件)
"workbench.iconTheme": "vscode-icons",
//打開檔案不覆寫
"workbench.editor.enablePreview": false,
// 設定不同檔案使用的格式化配置
"[html]": {
"editor.defaultFormatter": "esbenp.prettier-vscode"
},
"[javascript]": {
"editor.defaultFormatter": "vscode.json-language-features"
},
"[json]": {
"editor.defaultFormatter": "esbenp.prettier-vscode"
},
"[jsonc]": {
"editor.defaultFormatter": "vscode.json-language-features"
},
// 對vue的配置
"[vue]": {
"editor.defaultFormatter": "octref.vetur"
},
"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.ts": "prettier",
// vetur 縮進是4
"vetur.format.options.tabSize": 4,
"vetur.format.options.useTabs": false,
// #這個按使用者自身習慣選擇//格式化.vue中html
"vetur.format.defaultFormatter.html": "js-beautify-html",
// #vue元件中html代碼格式化樣式
"vetur.format.defaultFormatterOptions": {
"js-beautify-html": {
//屬性強制折行對齊
"wrap_attributes": "force-aligned"
}
},
"prettier.vueIndentScriptAndStyle": true,
}
eslint的官方說明
配置說明:https://eslint.bootcss.com/docs/user-guide/configuring#extending-configuration-files
Rules說明:https://eslint.bootcss.com/docs/rules/