• vue3-webpack遇到Eslint各种报错


    报错一:

    VUE3 You may use special comments to disable some warnings. Use // eslint-disable-next-line to ignor

    1. 8:1 error Delete `⏎` prettier/prettier
    2. 1 problem (1 error, 0 warnings)
    3. 1 error and 0 warnings potentially fixable with the `--fix` option

    原因:与创建项目时选择的 eslint 的设置问题,可以通过“—fix”选项修复
    .解决方法
    package.json
    //原代码

    1. "scripts": {
    2. ...
    3. "lint": "vue-cli-service lint"
    4. },

    更改:

    1. "scripts": {
    2. ...
    3. "lint": "eslint --fix --ext .js,.vue src"
    4. },

    报错二:
    提示建议我们使用特殊注释禁用某些警告。使用//eslint disable next line忽略下一行。使用/eslint disable/忽略文件中的所有警告。

    1. You may use special comments to disable some warnings.
    2. Use // eslint-disable-next-line to ignore the next line.
    3. Use /* eslint-disable */ to ignore all warnings in a file.

    解决办法:
    我们在 .eslintrc.js 里面注释掉 plugin:prettier/recommended 就可以了

    1. 'extends': [
    2. // 'plugin:vue/vue3-essential',
    3. 'eslint:recommended',
    4. '@vue/typescript/recommended'
    5. //'plugin:prettier/recommended'
    6. ],

    报错三:
    这个报错是建议我们使用 驼峰命名

    6:9  error  Component name "My" should always be multi-word  vue/multi-word-component-names

    解决办法:
    ① 按照规则走,改驼峰命名
    ②但是像以上我就一个 My ,这样要写个驼峰不是很合理,所以可以在 .eslintrc.js 文件写一条规则:

    1. rules: {
    2. "no-console": process.env.NODE_ENV === "production" ? "warn" : "off",
    3. "no-debugger": process.env.NODE_ENV === "production" ? "warn" : "off",
    4. // 关闭驼峰命名规则
    5. 'vue/multi-word-component-names': 0,
    6. },

    报错四:

    Error-Do not use “// @ts-ignore“ because it alters compilation errors问题的处理

    Error-Do not use “// @ts-ignore“ because it alters compilation errors问题的处理

    使用TS编写代码时,有些情况下,比如第三方的库对象,我们想增加一些属性,并且确认是没问题的,但是TS检查时会报错导致不能正常编译运行

    解决办法:

    我们通过添加// @ts-ignore来告诉TS该条语句不检查类型问题,此时是可以正常编译了,但是// @ts-ignore这条注释标红了很难受:
    这个我们可以通过修改.eslintrc.js文件来消除该提示:

    1. module.exports = {
    2. ...
    3. rules: {
    4. ...
    5. "@typescript-eslint/ban-ts-comment": "off",
    6. }
    7. }

    报错五:使用Vue3 Script Setup时 ESLint 报错 ‘defineProps‘ is not defined

    Vue 3 的 Script Setup 语法引入了 defineProps、defineEmits、defineExpose、withDefaults 的编译器宏。然而某些情况下,ESLint 会报错以上编译器宏函数未定义。

    本文将介绍两种解决方案来解决这个问题(假定你的项目使用 Vue-Cli 进行初始化)。

    Step 1. 检查 eslint-plugin-vue 的版本

    npm list eslint-plugin-vue
    

    若版本在 v8.0.0 以上,跳转到 Step 2,否则直接到 Step 3 的内容。

    Step 2. 版本为 v8.0.0+

    打开 .eslintrc.js 文件并修改如下:

    1. env: {
    2. node: true,
    3. // The Follow config only works with eslint-plugin-vue v8.0.0+
    4. "vue/setup-compiler-macros": true,
    5. },

    Step 3. 版本为 v8.0.0 以下

    打开 .eslintrc.js 文件并修改如下:

    1. // The Follow configs works with eslint-plugin-vue v7.x.x
    2. globals: {
    3. defineProps: "readonly",
    4. defineEmits: "readonly",
    5. defineExpose: "readonly",
    6. withDefaults: "readonly",
    7. },

     更多的Eslint编写参考如下:

    1. module.exports = {
    2. parser: '@typescript-eslint/parser',
    3. extends: [
    4. 'eslint:recommended',
    5. 'plugin:@typescript-eslint/recommended',
    6. 'prettier',
    7. 'plugin:prettier/recommended'
    8. ],
    9. env: {
    10. browser: true,
    11. es2021: true
    12. },
    13. extends: ['eslint:recommended'],
    14. plugins: ['@typescript-eslint', 'prettier'],
    15. parserOptions: {
    16. ecmaVersion: 'latest',
    17. sourceType: 'module'
    18. },
    19. rules: {
    20. 'prettier/prettier': 'error',
    21. 'no-extra-semi': 'off',
    22. '@typescript-eslint/camelcase': 'off',
    23. '@typescript-eslint/ban-ts-ignore': 'off',
    24. '@typescript-eslint/no-var-requires': 'off',
    25. '@typescript-eslint/no-extra-semi': 'off',
    26. '@typescript-eslint/no-explicit-any': 'off',
    27. '@typescript-eslint/no-empty-function': 'off',
    28. '@typescript-eslint/no-non-null-assertion': 'off',
    29. '@typescript-eslint/explicit-function-return-type': 'off',
    30. '@typescript-eslint/explicit-module-boundary-types': 'off',
    31. '@typescript-eslint/no-empty-interface': 'off'
    32. }
    33. }

    源头解决(不推荐)

    Vue 项目报错:‘XXXXX‘ is not defined ( no-undef ) 解决方法

    问题描识:使用vue的时候,使用一个全局变量或在当前方法调用别的方法,ESLint的语法会出现ESLint: ‘Aliplayer’ is not defined. (no-undef),说未定义,这时我们可以添加配置,取消这个校验。
    在node_modules文件夹下面的eslint文件夹下面的conf里面的eslint-recommended.js文件注释掉"no-undef": “error”,这行代码

    node_modules

            --eslint

                    --conf

                            --eslint-recommended.js

     

  • 相关阅读:
    Shell-05Shell三剑客之sed
    哈夫曼树--贪心算法
    告诉你,项目管理就用这一页纸
    vue3 + typescript 实操踩的坑
    【FPGA开发/IC开发之时序约束最全面的归纳总结】时序路径基本概念及时序约束分析方法
    释放潜能!RunnerGo:性能测试的全新视角
    设计模式-7种结构型模式
    vue3响应式
    从功能测试到自动化测试,我在阿里的这7年
    机械寿命预测(基于NASA C-MAPSS数据的剩余使用寿命RUL预测,Python代码,CNN_LSTM模型,有详细中文注释)
  • 原文地址:https://blog.csdn.net/qq_38567039/article/details/127800948