【发布时间】:2018-12-25 17:43:33
【问题描述】:
我尝试使用 mapState 函数从商店获取状态,但我无法使用生成的代码将值返回到我的模板代码中...
<template>
// Some code
<template v-if="!isLoggedIn">
// Some code
</template>
<template v-else>
// Some code
{{ currentUser.name }}
</template>
// Some code
</template>
<script>
import { mapState } from "vuex";
export default {
// Some code
computed: {
...mapState({auth : ['currentUser', 'isLoggedIn','customers']})
}
}
</script>
下面的代码可以工作
<script>
import { mapState } from "vuex";
export default {
// Some code
computed: {
currentUser() {
return this.$store.state.auth.currentUser
},
isLoggedIn() {
return this.$store.state.auth.isLoggedIn
},
}
}
</script>
警告信息
[Vue 警告]:属性或方法“isLoggedIn”未在实例上定义,但在渲染期间被引用。通过初始化该属性,确保此属性是反应性的,无论是在数据选项中,还是对于基于类的组件。请参阅:https://vuejs.org/v2/guide/reactivity.html#Declaring-Reactive-Properties。
提前致谢
【问题讨论】:
-
我们需要看看你的 Vuex store 和模块的基本结构。请和谢谢!
标签: javascript vue.js vuejs2 vuex