跳到主内容

Vue3 Script Setup defineProps is not defined, defineExpose is not defined

· 1分钟阅读

问题

新的项目使用了 Vue3 来开发,由于使用了新的 Script setup 语法,在 eslint 上居然报了以下错误

'defineProps' is not defined

原因

经过查找定位到,根本原因是 eslint-plugin-vue 的版本问题。首先我们检查 eslint-plugin-vue 的版本。

npm list --depth=0 eslint-plugin-vue

查看到版本是否大于 8.0.0。

解决

  • 如果 eslint-plugin-vue 的版本大于 8.0.0,则只需要修改 .eslintrc.js 文件即可。
{
env: {
node: true,
// The Follow config only works with eslint-plugin-vue v8.0.0+
"vue/setup-compiler-macros": true,
},
}
  • 如果 eslint-plugin-vue 的版本小于 8.0.0,则修改 .eslintrc.js 文件为。
{
// The Follow configs works with eslint-plugin-vue v7.x.x
globals: {
defineProps: "readonly",
defineEmits: "readonly",
defineExpose: "readonly",
withDefaults: "readonly",
}
}