【问题标题】:Vuex - mapGetters et al but mapping by parameterVuex - mapGetters 等,但按参数映射
【发布时间】:2019-08-17 09:05:03
【问题描述】:

我正在尝试创建一个通用过滤器组件,从商店获取数据

现在,我像这样使用mapGetters

...mapGetters({
  items: 'filters'
}),

但我希望能够将其概括为我正在映射的吸气剂

props: {
  filterType: String
},

computed: {
    ...mapGetters({
    items: this.filterType
  }),
}

这给了我以下错误

Cannot read property 'nombre' of undefined

我知道我能做到:

computed: {
  items: function () {
      return this.$store.getters[this.filterType]
  }
}

但我只是想检查是否有办法将 vue 实例的属性与 mapGetters 一起使用,或者您是否需要严格硬编码内部的 getters 名称

【问题讨论】:

    标签: vue.js vuex


    【解决方案1】:

    也许这就是您正在寻找的(或至少最接近使用mapGetters):

    props: {
       filterType: String
    },
    
    computed: {
      ...mapState({
        items(state, getters) {
          return getters[this.filterType];
        }
      })
    }
    

    【讨论】:

    • 知道如何在 Vuex 4 中使用一组吸气剂来做到这一点吗? filterType: ['getter1', 'getter2']
    • 很抱歉@WillyBurb,但大约一年半前我停止使用 Vue。我没有更新 Vue 或 Vuex 的最新版本...
    猜你喜欢
    • 2018-01-08
    • 2020-06-22
    • 1970-01-01
    • 2017-11-18
    • 2020-06-17
    • 2019-06-04
    • 1970-01-01
    • 2021-05-24
    • 1970-01-01
    相关资源
    最近更新 更多