【问题标题】:VuexFire: how to combine computed: and computed()VuexFire:如何结合计算:和计算()
【发布时间】:2018-01-14 23:10:07
【问题描述】:

我在一个应用程序中使用 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 的数据。

正确的做法是什么?

【问题讨论】:

    标签: firebase vue.js vuex


    【解决方案1】:

    首先,您将始终将计算属性指定为对象,而不是方法(就像您对 computed() 所做的那样)。

    其次,需要在computed对象中使用spread operator

    computed: {
      amethod() { return 1 },
      ...Vuex.mapGetters(['todos'])
    }
    

    这有效地分散了Vuex.mapGetters 返回的所有计算属性方法,并将它们定义为computed 对象的属性。

    【讨论】:

    • 非常感谢。不知道为什么我不试试这个!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-01-07
    相关资源
    最近更新 更多