【发布时间】:2020-03-29 17:59:17
【问题描述】:
我从另一个开发人员那里接管了一个代码。他使用了一个 Vuex 属性 POLL 来初始化一些组件。我想使用不同的方法——在上层获取对象并将其作为下游属性传递给组件。该对象是从后端以异步方法获取的。我认为 Vue 反应性稍后会初始化实际值。但我得到一个错误:
[Vue warn]: Property or method "poll" is not defined on the instance but referenced during render. Make sure that this property is reactive, either in the data option, or for class-based components, by initializing the property. See: https://vuejs.org/v2/guide/reactivity.html#Declaring-Reactive-Properties.
found in
---> <PollHeading> at src/components/molecules/PollHeading.vue
<CompletePoll> at src/components/organisms/CompletePoll.vue
<Home> at src/views/Home.vue
首页.vue
<Poll v-if="this.$store.getters.LATEST_POLL" :item="this.$store.getters.LATEST_POLL" />
...
created() {
this.$store.dispatch('GET_LATEST_POLL');
},
<PollHeading :item="item"/>
...
props: {
item: Object,
},
PollHeading.vue
props: {
item: Object,
},
我是不是走错了方向,而原来的开发者是对的?或者有没有办法解决我的方法?他曾经这样做:
PollHeading.vue(我改名为 CompletePoll.vue)
computed: {
poll() {
return {
poll: this.$store.getters.POLL,
};
},
【问题讨论】:
-
你试过
mapGetters吗?像这样的东西:computed: { ...mapGetters(['POLL']) }.