解决:使用 Vue 3 Script Setup 时 ESLint 报错 ‘defineProps‘ is not defined

解决:使用 Vue 3 Script Setup 时 ESLint 报错 ‘defineProps‘ is not defined,第1张

解决:使用 Vue 3 Script Setup 时 ESLint 报错 ‘defineProps’ is not defined

Vue 3 的 Script Setup 语法引入了 definePropsdefineEmitsdefineExposewithDefaults 的编译器宏。然而某些情况下,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 文件并修改如下:

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

Step 3. 版本为 v8.0.0 以下

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

  // The Follow configs works with eslint-plugin-vue v7.x.x
  globals: {
    defineProps: "readonly",
    defineEmits: "readonly",
    defineExpose: "readonly",
    withDefaults: "readonly",
  },

若你的 eslint-plugin-vue 版本在 v8 以下,不建议贸然升级版本(尤其是在使用了大量 ts 依赖时)。


参考链接 vue3报错:‘defineProps‘ is not defined,‘defineExpose‘ is not definedCompiler macros such as defineProps and defineEmits generate no-undef warningseslint compile time error - Environment key “vue/setup-compiler-macros” is unknownWhat is causing Error: .eslintrc.js: Environment key “vue/setup-compiler-macros” is unknown

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1298986.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-06-10
下一篇 2022-06-10

发表评论

登录后才能评论

评论列表(0条)

保存