You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.15 15:01:04