I've mapped a set of getters into my component and am trying to call them with a parameter in methods, however the getters are coming up as undefined. I've mapped them following an answer on a 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)
},
}
The error being:
TypeError: this.categories/getCategory is not a function
I've console logged this
:
Edit: Updated code following @Luceos answer:
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)
},
}
Which returns:
TypeError: this.getCategory is not a function
My getter:
getCategory: (state, id) => {
return state.categories.filter(category => category.id === id);
}
Try this:
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)
},
}
And your getters should be functions expecting the id for example:
getCategory: (state) => (id) => {
return state.categories.filter(category => category.id === id);
}
Make sure to take a look at docs Method-Style Access