Vue3+Pinia项目开发正常但构建报错:Cannot read properties of undefined
问题排查:Vue3 + Pinia构建时出现TypeError: Cannot read properties of undefined (reading 'setTheResID')
错误现象
- 抛出错误:
TypeError: Cannot read properties of undefined (reading 'setTheResID') - 组件内定义Pinia实例时,变量
store1被IDE标灰,未被识别:import {useemarresidents} from '../store/emaresidents.js' const store1 = useemarresidents() - 调用方法时的矛盾情况:
- 使用
this.store1.setTheResID(this.ResidentID)触发上述错误 - 移除
this后,提示store1未定义
- 使用
Pinia Store的Actions代码
actions: { setTheResID(setResident) { this.currResID = setResident; }, }
核心疑问
- 开发环境运行正常,但执行
vite build构建发布时失败,为何两个环境表现不一致? - 曾怀疑是Vue3与Pinia3版本兼容问题,但实际依赖版本为Vue 3.2.41、Pinia 2.0.21
- 额外问题:命令行无法识别
vue和pinia命令
项目package.json内容
{ "name": "aplifyvue", "version": "0.0.0", "scripts": { "dev": "vite", "build": "vite build", "preview": "vite preview --port 4173" }, "dependencies": { "@mdi/font": "^7.0.96", "axios": "^1.1.3", "pinia": "^2.0.21", "vue": "^3.2.41", "vue-router": "^4.1.5", "wave-ui": "^2.43.0" }, "devDependencies": { "@vitejs/plugin-vue": "^3.0.3", "vite": "^3.0.9" } }
内容的提问来源于stack exchange,提问作者Bill
相关产品推荐
相关产品推荐

