esLint基础使用
1)修改vscode 设置
修改tab空格设置;tabsize配置项为2
format 自动保存代码
2)在vue创建项目中,选择eslint+Standard config -标准规范-
在保存时就开启检查
3)语法报错,根据错误提示,去eslint官网搜索规则
4)在eslintrc.js文件中 extends就是官方提供的默认规则
5)在eslintrc.js文件中rules中添加或者修改规则,自定义的规则大于extends的规则
“规则”:“开启/关闭”
“no-extra-semi”:“off” //禁止不必要的分号: 关闭
1)off或者0 关闭规则
2)warn或者1 开启规则,使用警告级别,程序继续执行
3)error或者2 开启规则,使用错误级别,程序报错关闭
没有属性的规则,只需要控制开启,还是关闭;例如:“eqeqeq”: “off”,
有属性的规则可以使用属性;
例如:“semi”: [“开启/关闭”, “属性”]
semi规则有两个属性,always(默认,要求在语句末尾使用分号),never(禁止在语句末使用分号)
“semi”:[“error”, “always”] //语句末没有分号就报错
如使用 window 对象,默认情况下会报 no-undef 的错误,需要在 .eslintrc 中进行相应配置
{
"rules": {
// ...
},
"globals": {
"window": true
}
}
6)使用vscode插件
eslint
//eslint 插件配置
"editor.codeActionsOnSave": {
"source.fixAll": true
},
"eslint.alwaysShowStatus": true,
prettier插件
//prettier插件配置
//prettier插件配置
"prettier.trailingComma": "none",
"prettier.semi": false,
//每行文字上线,超过换行
"prettier.printWidth": 300,
//使用单引号替换双引号
"prettier.singleQuote": true,
"prettier.arrowParens": "avoid",
vetur插件
"vetur.format.defaultFormatter.html": "js-beautify-html",
"vetur.ignoreProjectWarning": true,
"vetur.format.defaultFormatterOptions": {
"prettier":{
"trailingComma":"none",
"semi":false,
"singleQuote":true,
"arrowParens":"avoid",
"printWidth":300
},
"js-beautify-html": {
"wrap_attributes":false
}
},
7)在用户目录下创建.prettier文件,添加以下内容
"trailingComma":"none",
"singleQuote": true,
"semi": false,
"arrowParens": "avoid"
在vscode配置文件中添加: “prettier.configPath”:“地址\.prettierrc”
在vscode中右键选择文档格式化方式,选择prettier-code formatter
其他文章https://www.jianshu.com/p/ad1e46faaea2