相信很多小伙伴在从事前端开发苦eslint代码规范化久矣,下面介绍一个方法可以解决这个烦恼
1、在自己的vscode编辑器中安装ESlint插件和Prettier插件
2、在设置中的settings.json添加这三行代码
"editor.formatOnType": true,
"editor.formatOnSave": true,
"editor.formatOnPaste": true,
3、配置项目文件中.eslintrc.json和.prettierrc.js文件(没有的可以在项目根目录创建这两个文件)
.eslintrc.json配置如下
{
"env": {
"browser": true,
"es6": true,
"node": true,
"commonjs": true,
"jest": true
},
"parser": "@typescript-eslint/parser",
"parserOptions": {
"ecmaVersion": 2017,
"sourceType": "module",
"ecmaFeatures": {
"jsx": true,
"legacyDecorators": true
}
},
"extends": [
"eslint:recommended",
"plugin:@typescript-eslint/eslint-recommended",
"prettier/@typescript-eslint",
"plugin:prettier/recommended",
"prettier/react",
"plugin:react-hooks/recommended"
],
"plugins": ["@typescript-eslint", "prettier", "react", "react-hooks"],
&