什么导致了错误:.eslintrc.js:环境键“vue/setup-compiler-macros”是未知的。

9

我正在使用Vue 3和Typescript开发一个示例应用程序。具体而言,我正在使用Vue SFC中的新Vue v3.2设置选项。Vue文档建议我将“vue/setup-compiler-macros”添加到eslintrc.js文件的env部分,这是有效的。但现在我遇到了一个错误。

Syntax Error: Error: .eslintrc.js:
        Environment key "vue/setup-compiler-macros" is unknown
    at Array.forEach (<anonymous>)

有段时间,如果我重新启动了VS Code(我承认这不是一个很好的解决办法),这个问题似乎就消失了,但现在即使这样也不行了。当我保存一个文件并且项目被编译时就会出现这个错误。

我似乎正在使用VS Code扩展程序-ESLint v2.2.2。

eslintrc.js:

  module.exports = {
  root: true,
  env: {
    'vue/setup-compiler-macros': true,
    node: true,
  },
  extends: [
    'plugin:vue/vue3-essential',
    'eslint:recommended',
    '@vue/typescript/recommended',
  ],
  parserOptions: {
    ecmaVersion: 2020,
  },
  rules: {
    'no-console': process.env.NODE_ENV === 'production' ? 'warn' : 'off',
    'no-debugger': process.env.NODE_ENV === 'production' ? 'warn' : 'off',
  },
}

非常感谢您的建议。


附加信息: "@typescript-eslint/eslint-plugin": "^4.18.0", "@typescript-eslint/parser": "^4.18.0", "@vue/cli-plugin-babel": "~4.5.0", "@vue/cli-plugin-eslint": "^4.5.15", "@vue/cli-plugin-router": "~4.5.0", "@vue/cli-plugin-typescript": "~4.5.0", "@vue/cli-service": "~4.5.0", "@vue/compiler-sfc": "^3.0.0", "@vue/eslint-config-typescript": "^7.0.0", "apollo": "^2.33.7", "eslint": "^6.7.2", "eslint-plugin-vue": "^7.0.0", "typescript": "~4.1.5" }``` - maxweld
4个回答

12

对我来说,执行这个操作并添加 "vue/setup-compiler-macros": true 标志是有效的。 - Chris O'Sullivan

11

我也遇到了同样的问题,通过配置解决了

  globals: {
    defineProps: "readonly",
    defineEmits: "readonly"
  }

这里有官方指南,我不知道为什么'vue/setup-compiler-macros': true,不能工作


谢谢 - 那很有效。我也已经提了一个关于 ESLinter 的 bug 报告。 - maxweld
1
"'vue/setup-compiler-macros': true" 是有效的,你需要将这行代码添加到环境变量中,而不是规则中。 - n4ks
1
根据另一个答案,你需要 eslint-plugin-vue ^8.0.0 版本才能让 env 标志起作用。此外,对于这个答案的解决方案,你可能还想添加 withDefaults 和其他你正在使用的宏。 - V. Rubinetti

2
您可以查看这个答案,它帮助我正确解决了这个问题。
您基本上需要做以下操作:
  1. 在终端上运行npm uni -D babel-eslint来删除babel-eslint
  2. 在终端上运行npm i -D @babel/eslint-parser来安装@babel/eslint-parser
  3. 使用以下内容更新您的ESLint配置文件中的env行(可能在.eslintrc.js.eslintrc.jsonpackage.json中):
  env: {
    node: true,
    'vue/setup-compiler-macros': true,
  },
  1. 请在您的ESLint配置中更新parserOptions行,内容如下:
  parserOptions: {
    parser: '@babel/eslint-parser',
    ecmaVersion: 2018,
    requireConfigFile: false, // This will prevent Babel from looking for a config file you possibly don’t have or need.
  },
  1. 如果在 parserOptions 之外有 parser 行,则可以将其删除以避免冲突。

1

使用配置创建eslintrc.is文件对我没有起作用。

我在不运行任何升级的情况下修复了这个错误,我所做的就是在package.json文件的eslintConfig部分添加了"vue/setup-compiler-macros: true"。

即:

"eslintConfig": {
    ...
    "env": {
        node: true,
        "vue/setup-compiler-macros": true
    }
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接