vue项目eslint+prettier 统一代码风格
文章目录
前言
一、
(方案一)
说明:eslint+prettier-让prettier使用eslint的代码格式进行校验
步骤:
1.安装vscode插件
Vetur
ESLint
Prettier - Code formatter
2.配置vscode设置
{
// 控制工作台中活动栏的可见性。
"workbench.activityBar.visible": true,
//主题设置
"workbench.colorTheme": "Monokai",
// 默认编辑器字号
"editor.fontSize": 14,
//是否自动换行
"editor.wordWrap": "on",
"workbench.editor.enablePreview": false, //打开文件不覆盖
"search.followSymlinks": false, //关闭rg.exe进程
"editor.minimap.enabled": false, //关闭迷你图快速预览
"files.autoSave": "onWindowChange", // 切换窗口时自动保存。
"editor.lineNumbers": "on", //开启行数提示
"editor.quickSuggestions": {
//开启自动显示建议
"other": true,
"comments": true,
"strings": true
},
"editor.tabSize": 2, //制表符符号eslint
//.vue文件template格式化支持,并使用js-beautify-html插件
"vetur.format.defaultFormatter.html": "js-beautify-html",
//js-beautify-html格式化配置,属性强制换行
"vetur.format.defaultFormatterOptions": {
"js-beautify-html": {
"wrap_attributes": "force-aligned"
}
},
//根据文件后缀名定义vue文件类型
"files.associations": {
"*.vue": "vue"
},
//配置 ESLint 检查的文件类型
"eslint.validate": [
"javascript",
"javascriptreact",
"html",
"vue"
],
//保存时eslint自动修复错误
"editor.codeActionsOnSave": {
"source.fixAll.eslint": true
},
//保存自动格式化
"editor.formatOnSave": true
}
3.配置.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',
//强制使用单引号
quotes: ['error', 'single'],
//强制不使用分号结尾
semi: ['error', 'never']
},
parserOptions: {
parser: 'babel-eslint'
}
}
4.配置.prettierrc
{
"eslintIntegration": true, // #让prettier使用eslint的代码格式进行校验
"singleQuote": true, //单引号包含字符串
"semi": false, //不添加末尾分号
}
(方案二)
说明:vue+vetur+eslint+prettier自动格式化功能
步骤:
步骤1,2同上(暂未四选一,未测试)
1配置参考
2配置大全
3segmentfault配置
4参考配置
二、使用步骤
1.简介完整配置版本*****
注意:出现的报错:
(1)在vscode中配制setting.json遇到“Auto Fix is enabled by default. Use the single string form.”报错解决
(2)vscode的elsint配置报错“The setting is deprecated. Use editor.codeActionsOnSave instead with a source”
2.读入数据
三、关闭校验
四、常见问题
如果你使用的css预处理器是stylus,那么还需要加入以下配置
Vetur插件用于识别vue文件,原本vetur自带格式化功能,但是和eslint会有冲突
prettier不能格式化vue文件template 所以使用js-beautify-html格式化
五、set.json中初始化配置
{
"[vue]": {
"editor.defaultFormatter": "esbenp.prettier-vscode"
},
"editor.codeActionsOnSave": null
}
现在的版本
{
"[vue]": {
"editor.defaultFormatter": "octref.vetur"
},
"editor.codeActionsOnSave": {
"source.fixAll": true
},
"editor.formatOnSave": true,
"editor.fontLigatures": null
}