安装 vs code 和插件
- Vetur
- ESLint
- Prettier - Code formatter
settings.json
{
//.vue文件template格式化支持,并使用js-beautify-html插件
"vetur.format.defaultFormatter.html": "js-beautify-html",
//js-beautify-html格式化配置,属性强制换行
//文档:https://github.com/beautify-web/js-beautify
"vetur.format.defaultFormatterOptions": {
"js-beautify-html": {
"wrap_attributes": "force"
}
},
//根据文件后缀名定义vue文件类型
"files.associations": {
"*.vue": "vue"
},
//保存时eslint自动修复错误
"eslint.validate": [
"javascript",
"javascriptreact",
{
"language": "vue",
"autoFix": true
}
],
"eslint.autoFixOnSave": true
}
ESLint 和 Prettier 的冲突修复
// .eslintrc.js
module.exports = {
root: true,
env: {
node: true
},
extends: ["plugin:vue/essential", "eslint:recommended"],
rules: {
"no-console": process.env.NODE_ENV === "production" ? "error" : "off",
"no-debugger": process.env.NODE_ENV === "production" ? "error" : "off",
"no-alert": process.env.NODE_ENV === "production" ? "error" : "off",
//强制使用单引号
quotes: ["error", "single"],
//强制不使用分号结尾
semi: ["error", "never"]
},
parserOptions: {
parser: "babel-eslint"
}
};
// .prettierrc
{
"eslintIntegration": true,
//使用单引号
"singleQuote": true,
//结尾不加分号
"semi": false
}