VuexFire:如何结合计算:和计算()
VuexFire: how to combine computed: and computed()
我在一个应用程序中使用 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 的数据不适用于该方法。
正确的做法是什么?
首先,您将始终将计算属性指定为对象,而不是方法(就像您在 computed()
中所做的那样)。
其次,你需要在computed
对象中使用spread operator:
computed: {
amethod() { return 1 },
...Vuex.mapGetters(['todos'])
}
这有效地展开了 Vuex.mapGetters
返回的所有计算 属性 方法,并将它们定义为 computed
对象的属性。
我在一个应用程序中使用 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 的数据不适用于该方法。
正确的做法是什么?
首先,您将始终将计算属性指定为对象,而不是方法(就像您在 computed()
中所做的那样)。
其次,你需要在computed
对象中使用spread operator:
computed: {
amethod() { return 1 },
...Vuex.mapGetters(['todos'])
}
这有效地展开了 Vuex.mapGetters
返回的所有计算 属性 方法,并将它们定义为 computed
对象的属性。