【发布时间】: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 应该是接受参数的高阶函数。