首页 > 解决方案 > 为什么在 vuejs 应用程序中,vuejs 应用程序中的 prefer-const 规则没有被禁用?

问题描述

在@vue/cli 应用程序中出现错误 eslint 错误:

  117:11  error  'resArray' is never reassigned. Use 'const' instead     prefer-const
  
  pointing at line :
      let resArray = []
  

我通过在 package.json 添加规则 prefer-const = 0 禁用了这个错误:

{
  "name": "yt3",
  "version": "0.1.0",
  "private": true,
  "scripts": {
    "serve": "vue-cli-service serve",
    "build": "vue-cli-service build",
    "lint": "vue-cli-service lint"
  },
  "dependencies": {
    "axios": "^0.20.0-0",
    "core-js": "^3.6.5",
    "mitt": "^2.1.0",
    "moment-timezone": "^0.5.31",
    "node-sass": "^4.12.0",
    "sass-loader": "^10.0.4",
    "vee-validate": "^3.1.0",
    "vue": "^3.0.0",
    "vue-router": "^4.0.0-rc.1",
    "vuex": "^4.0.0-rc.1"
  },
  "devDependencies": {
    "@vue/cli-plugin-babel": "~4.5.0",
    "@vue/cli-plugin-eslint": "~4.5.0",
    "@vue/cli-service": "~4.5.0",
    "@vue/compiler-sfc": "^3.0.0",
    "@vue/eslint-config-standard": "^5.1.2",
    "babel-eslint": "^10.1.0",
    "eslint": "^6.7.2",
    "eslint-plugin-import": "^2.20.2",
    "eslint-plugin-node": "^11.1.0",
    "eslint-plugin-promise": "^4.2.1",
    "eslint-plugin-standard": "^4.0.0",
    "eslint-plugin-vue": "^7.0.0-0"
  },
  "eslintConfig": {
    "root": true,
    "env": {
      "node": true
    },
    "extends": [
      "plugin:vue/essential",
      "eslint:recommended"
    ],
    "parserOptions": {
      "parser": "babel-eslint"
    },
    "rules": {
      "semi": [
        2,
        "never"
      ],
      "promise/param-names": [
        0
      ],
      "dot-notation": [
        0
      ],
      "no-trailing-spaces": [
        0
      ],
      "prefer-const": [
        0
      ]
    }
  }
}

并运行命令:

yarn run serve

我预计最后一个值为 0 的条件将禁用此错误,但失败了。哪个是有效的语法?

我有文件: .eslintrc.js 行:

module.exports = {
  root: true,
  env: {
    node: true
  },
  extends: [
    'plugin:vue/vue3-essential',
    '@vue/standard'
  ],
  parserOptions: {
    parser: 'babel-eslint'
  },
  rules: {
    'no-console': process.env.NODE_ENV === 'production' ? 'warn' : 'off',
    'no-debugger': process.env.NODE_ENV === 'production' ? 'warn' : 'off'
  }
}

我要编辑它吗?这个怎么运作 ?

谢谢!

标签: vue.jseslint

解决方案


If you already have a .eslintrc.js file I would suggest moving all your eslint config into there to prevent any conflicts with the package.json file.

You should be able to add 'prefer-const': 'off' to your rules to disable that error.

Although if you are only using resArray with array operations e.g. .push(), .pop(), .splice() etc and are not going to reassign the value e.g resArray = [] then you can use const resArray = [] instead without needing to remove the prefer-const rule.


推荐阅读