我在一个应用程序中使用VuexFire,与在https://github.com/posva/vuexfire中一样,在我的组件中,我有如下代码行
computed: Vuex.mapGetters(['todos']),
created() {
this.$store.dispatch('setTodosRef', todosRef)
},这种方法工作得很好,除非我还有一些计算方法,在这种情况下我不知道该怎么做。我尝试过这样的东西:
computed: Vuex.mapGetters(['todos']),
computed() {
amethod: { return 1 }
},
created() {
this.$store.dispatch('setTodosRef', todosRef)
},和
computed() {
amethod: { return 1 }
},
created() {
...Vuex.mapGetters(['todos'])
this.$store.dispatch('setTodosRef', todosRef)
},但这些,以及我尝试过的其他事情,显然是被误导的,因为它们不起作用(即来自firebase的数据对该方法不可用)。
正确的方法是什么?
发布于 2017-08-07 23:40:18
首先,您将始终将计算属性指定为对象,而不是方法(就像对computed()所做的那样)。
其次,您需要在computed对象中使用spread operator:
computed: {
amethod() { return 1 },
...Vuex.mapGetters(['todos'])
}这有效地展开了Vuex.mapGetters返回的所有计算属性方法,并将它们定义为computed对象的属性。
https://stackoverflow.com/questions/45550392
复制相似问题