vue2页面内使用计算属性获取store的值,当内容更改时,会报错
页面部分
<u-fab :draggable="true" @click="onBtnClick" v-if="isLogin">
<template #trigger>
<view class="me-box me-orient create-box me-pack me-align">
<image src="@/assets/images/icon/icon-create.png" mode="widthFix" class="create"></image>
<text class="ftz12 t-ffe9cb mar-t-5">发布</text>
</view>
</template>
</u-fab>
js部分
computed: {
isLogin() {
return store.getters.isLogin
}
},
每次登录后,修改store里的isLogin字段,页面触发更新,会一直报错
Uncaught TypeError: Cannot read properties of undefined (reading '$') at uni-app-view.umd.js:7
换了好几种获取方式都报
帮忙给孩子看看吧,谢谢了
1 个回复
Ask小助手 - Ask社区AI助手
根据您的问题描述,Vue2页面使用计算属性获取store变量在真机报错,错误信息显示"Cannot read properties of undefined (reading '$')",这通常是由store未正确挂载或访问方式不当引起的。
解决方案:
优先使用
this.$store而非直接导入store直接导入store实例(
import store from '@/store')在部分真机环境可能无法保证响应性,而this.$store能更好地利用Vue响应系统。确保store正确初始化
在main.js中必须挂载store:
检查store定义完整性
真机调试建议
console.log('store:', this.$store)特别注意: uni-app在真机环境下对store的响应性处理与H5环境有差异,直接导入store实例的方式在某些平台可能失效。官方文档明确推荐使用
this.$store方式访问,详见Getter核心概念。若仍无法解决,请检查是否在其他地方修改了Vue原型或存在store初始化时序问题。
要回复问题请先登录或注册
公告
更多>相关问题