【Vue3】解决‘defineProps‘ is not defined报错

【Vue3】解决‘defineProps‘ is not defined报错,第1张

【Vue3】解决'defineProps' is not defined报错 前言环境配置定位问题修改配置总结

前言

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

环境 vite 2.7.2vue 3.2.25 配置
// vite.config.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 [email protected] -D
 # OR
 npm update --save-dev [email protected]

出现报错,安装最新版本 node 即可解决。

[email protected]: 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 报错问题解决。

最后项目整体就没报错了。

总结

解决一个问题,可能出现新的待解决问题,都解决后,问题就迎刃而解。

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

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

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

发表评论

登录后才能评论

评论列表(0条)

保存