【发布时间】:2020-06-26 13:41:25
【问题描述】:
在我的组件中,我试图从我的 auth 模块访问 getter。
这是store/auth.js中的getter函数
getters: {
isAuthenticated: (state) => !!state.token,
},
这就是我尝试访问和显示这些数据的方式
// html in template of component
<span
v-if='this.isAuthenticated'
class='font-weight-bold grey--text text--darken-3'>
Hello
</span>
-------------------
// computed properties of component
computed: {
...mapGetters([
'auth/isAuthenticated',
]),
},
在开发工具中,这是我得到的错误 ' 属性或方法“isAuthenticated”未在实例上定义,但在渲染期间被引用。' 我真的不知道为什么也不知道如何访问这些数据,在 Vue 调试器中我可以看到 getter 工作并且返回 true。
我已经尝试过其他方式来访问数据,例如
isAuthenticated: () => this.$store.getters.isAuthenticated // and this.$store.getters.auth.isAuthenticated
但是当我尝试访问模板中的计算函数时,这会给出一个不同的错误typeError: Cannot read property '$store' of undefined 。
这真的很烦人,因为据我所知,我正在正确地尝试访问商店但它无法正常工作。
非常感谢您的解释。谢谢。
【问题讨论】:
标签: vue.js vuejs2 vuex vuex-modules