【问题标题】:Mapped getters are undefined映射的 getter 未定义
【发布时间】:2023-04-10 05:44:01
【问题描述】:

我已将一组 getter 映射到我的组件中,并尝试在方法中使用参数调用它们,但是 getter 出现时未定义。我已经根据previous question 上的答案对它们进行了映射

computed: {
    ...mapGetters([
        'products/getCategoryProducts', 
        'categories/getSubcategories', 
        'categories/getCategory'
    ]),

    products () {
        return this['products/getCategoryProducts'](this.id)
    },

    subCategories () {
        return this['categories/getSubcategories'](this.id)
    },

    category () {
        return this['categories/getCategory'](this.id)
    },
}

错误是:

TypeError: this.categories/getCategory 不是函数

我已经在控制台登录了this

编辑:@Luceos 回答后更新代码:

computed: {
    ...mapGetters({
        getProducts: 'products/getCategoryProducts', 
        getSubCategories: 'categories/getSubcategories', 
        getCategory: 'categories/getCategory'
    }),

    products () {
        return this.getProducts(this.id)
    },

    subCategories () {
        return this.getSubCategories(this.id)
    },

    category () {
        return this.getCategory(this.id)
    },
}

返回:

TypeError: this.getCategory 不是函数

我的吸气剂:

getCategory: (state, id) => {
    return state.categories.filter(category => category.id === id);
}

【问题讨论】:

  • 你能告诉我们商店里的getter代码吗?
  • 将我的吸气剂添加到问题中
  • 所以如果你的getter是getCategory你应该使用` ...mapGetters({ getCategory: 'getCategory' }),`
  • 尝试像这样制作getter:getCategory: (state) => (id) => { return state.categories.filter(category => category.id === id); }
  • @roliroli - 看起来正确,getter 应该是接受参数的高阶函数。

标签: vue.js vuejs2 vuex


【解决方案1】:

试试这个:

computed: {
    ...mapGetters({
        products: 'products/getCategoryProducts', 
        subcategories: 'categories/getSubcategories', 
        category: 'categories/getCategory'
    }),

    products () {
        return this.products(this.id)
    },

    subCategories () {
        return this.subcategories(this.id)
    },

    category () {
        return this.category(this.id)
    },
}

您的 getter 应该是期望 id 的函数,例如:

getCategory: (state) => (id) => {
    return state.categories.filter(category => category.id === id);
}

请务必查看文档Method-Style Access

【讨论】:

  • 这并没有考虑到传递给 getter 的参数。方法名称与映射的 getter 相同可能会导致冲突,对吗?
  • 更新了我的答案。请尝试一下,如果对你有用,请告诉我
【解决方案2】:

根据documentation,您可以重构它以使用对象样式访问器:

...mapGetters({
  // map `this.doneCount` to `this.$store.getters.doneTodosCount`
  doneCount: 'doneTodosCount'
})

所以在你的情况下:

computed: {
    ...mapGetters('products', {
        products: 'getCategoryProducts'
    }),
    ...mapGetters('categories', {
        subCategories: 'getSubcategories', 
        category: 'getCategory'
    }),
}

然后使用它(假设那些 getter 有参数):

this.products(this.id)

我看过shopping cart example,这是他们的sn-p,更新了上面的匹配:

...mapGetters('cart', {
      products: 'cartProducts',
      total: 'cartTotalPrice'
    })

【讨论】:

  • 我已经更新了我的问题,但是映射的 getter 现在未定义
  • 如果您愿意,您可以返回 mapGetters 类别一上的数组表示法
  • 是的,我仍然收到this,getCategory is not a function,所以它没有看到映射方法
  • 没有this.getCategory,我们将其映射到category。请参见上面的sn-p。
  • 不,我将它映射到getCategory 以防止与其他方法名称发生冲突。但是我已经解决了,我的吸气剂设置不正确
猜你喜欢
  • 2021-11-02
  • 2021-01-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-03-22
  • 2020-10-05
  • 1970-01-01
  • 2015-03-04
相关资源
最近更新 更多