Vue3报错‘defineProps‘ is not defined的解决方法
作者:harmsworth2016
前言
2021年结束了,Vite 的版本也升级了,现在试试新版 Vite 搭建 Vue 项目。
按照 vue3一步一步的详细讲解配置ESLint 中 vue 官方推荐安装 ESLint 的方式安装 Eslint,结果发现 'defineProps' is not defined 报错,现在来解决这个问题。

环境
vite 2.7.2vue 3.2.25
配置
// .eslintrc.js
module.exports = {
root: true,
env: {
node: true
},
extends: [
'plugin:vue/vue3-essential',
'@vue/standard'
],
parserOptions: {
ecmaVersion: 2020
},
rules: {
'no-console': process.env.NODE_ENV === 'production' ? 'warn' : 'off',
'no-debugger': process.env.NODE_ENV === 'production' ? 'warn' : 'off'
}
}
安装 ESlint 后
// package.json
{
"name": "my-vue-app1",
"version": "0.0.0",
"scripts": {
"build": "vite build",
"lint": "vue-cli-service lint",
"dev": "vite",
"preview": "vite preview"
},
"dependencies": {
"vue": "^3.2.25"
},
"devDependencies": {
"@vitejs/plugin-vue": "^2.0.0",
"@vue/cli-plugin-eslint": "~4.5.0",
"@vue/eslint-config-standard": "^5.1.2",
"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",
"vite": "^2.7.2"
}
}
定位问题
defineProps 属于 Vue3 的规则校验,需要在 eslint-plugin-vue官方指南中寻找对应配置。

添加对应配置即可
修改配置
// .eslintrc.js
module.exports = {
root: true,
env: {
node: true,
'vue/setup-compiler-macros': true
},
extends: [
'plugin:vue/vue3-essential',
'@vue/standard'
],
parserOptions: {
ecmaVersion: 2020
},
rules: {
'no-console': process.env.NODE_ENV === 'production' ? 'warn' : 'off',
'no-debugger': process.env.NODE_ENV === 'production' ? 'warn' : 'off'
}
}
结果新出现了一个报错 Environment key "vue/setup-compiler-macros" is unknown

根据提示可知,是当前依赖包 eslint-plugin-vue 中没有 vue/setup-compiler-macros 规则,故需升级 eslint-plugin-vue,当前最新版本是 8.4.0
yarn upgrade eslint-plugin-vue@8.4.0 -D # OR npm update --save-dev eslint-plugin-vue@8.4.0
出现报错,安装最新版本 node 即可解决。

eslint-plugin-vue@8.4.0: The engine “node” is incompatible with this module. Expected version “^12.22.0 || ^14.17.0 || >=16.0.0”. Got “14.15.0”
当安装成功后,Environment key "vue/setup-compiler-macros" is unknown 报错问题解决。

最后项目整体就没报错了。
总结
解决一个问题,可能出现新的待解决问题,都解决后,问题就迎刃而解。
到此这篇关于Vue3报错‘defineProps‘ is not defined的解决方法的文章就介绍到这了,更多相关Vue3 ‘defineProps‘ is not defined内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!
