eslint使用

  • 在vscode的配置
  1. 下载eslint插件
  2. 下载eslint全局插件
js 复制代码
npm install -g eslint

// 执行命令
esllint --init // 根据需要选择即可
  1. 配置eslint
js 复制代码
"eslint.enable": true, // 启用/禁用ESLint。
    "eslint.alwaysShowStatus": true,
    "eslint.debug": true, // 启用ESLint的调试模式
    "eslint.format.enable": true, // 启用ESLint作为已验证文件的格式化程序 快速修复
    "eslint.lintTask.enable": true,
    "editor.codeActionsOnSave": {
        // "source.fixAll.eslint": true // 设置保存时自动更改eslint错误
    },
    "eslint.codeAction.showDocumentation": {
        "enable": true
    },
    "eslint.options": { //指定eslint配置文件位置
        "configFile": ".eslintrc.js" //指定项目根目录中的eslint配置文件
    },
    "eslint.validate": [
        "javascript", "javascriptreact", "html", "vue", "typescript", "jsx",
    ],
}

更多在vscode中关于eslint的配置可查看文档

  • 在项目中的配置 在根目录添加.eslintrc.js .eslintignore两个文件
js 复制代码
// 在eslintrc.js中 这是vue的 eslint --init 都会自带的有 
module.exports = {
    root: true,
    "env": {
        "browser": true,
        "es2020": true
    },
    "parserOptions": {
        "ecmaVersion": 12,
        "sourceType": "module"
    },
    "plugins": [
        "vue"
    ],
    'extends': [
        'plugin:vue/essential',
        '@vue/standard',
        "eslint:recommended",
        "plugin:vue/essential"
    ],
    rules: { // 对项目进行规则控制
        // allow async-await
        // 'generator-star-spacing': 'off',
        // allow debugger during development
        // 'no-debugger': process.env.NODE_ENV === 'production' ? 'error' : 'off',
        // 'vue/no-parsing-error': [2, { 'x-invalid-end-tag': false }],
        // 'no-undef': 'off',
        // 'camelcase': 'off'
    },
    parserOptions: {
        parser: 'babel-eslint'
    }
};


// 在.eslintignore 中
/src/*.js // 写路径即可

在vue.config.js 中开启项目的eslint

js 复制代码
// Type: boolean | 'warning' | 'default' | 'error'
module.exports = {
  lintOnSave: process.env.NODE_ENV !== 'production',
  
  devServer: {//也可以通过设置让浏览器 overlay 同时显示警告和错误:
    overlay: {
      warnings: true,
      errors: true
    }
  } 
}

相关文档

  1. vue-loader.vuejs.org/zh/guide/li...
  2. eslint.org/docs/user-g...
  3. eslint.cn/
  4. github.com/vuejs/vue-d...
  5. github.com/ecmadao/Cod...

有错之处 还请不吝赐教 万分感谢

相关推荐
去伪存真1 个月前
ESLint + Husky 如何只扫描发生改动的文件?
前端·eslint
孟陬2 个月前
利用 caniuse 结合 browserslist 对 JS 做兼容性检测
typescript·eslint·trae
USER_A0012 个月前
【VUE3】Eslint 与 Prettier 的配置
vue3·eslint·prettier
去伪存真2 个月前
给接手历史项目配置ESLint+Prettier,发现还是会踩坑
前端·eslint
卡列尼娜翠花3 个月前
vscode 导入语句排序和删除未使用的导入
前端·javascript·vscode·编辑器·eslint·前端工程化·esm
曾富贵3 个月前
【eslint 插件】导入语句排序
前端·eslint
bysking3 个月前
【24 Eslint 9.x插件】Eslint插件编写例子-bysking
前端·eslint
任磊abc3 个月前
nextjs15简要介绍以及配置eslint和prettier
eslint·nextjs·prettier·nextjs15
东东同学3 个月前
UmiJS PC 项目三:一场由 TypeScript 类型引发的"血案"
eslint·前端工程化