Answer the question
In order to leave comments, you need to log in
How to make a getter function?
The vuex store has an array of categories and an array of posts with the id of the category it belongs to:
{
posts: [
{id: 1, catId: 1, title: 'Foo'},
//...
],
categories: [
{id: 1, title: 'CatFoo'}
]
}
Answer the question
In order to leave comments, you need to log in
As a result, I wrote something like this:
getters: {
getPostsByCategory(state) {
const map = new Map();
state.posts; // Необходимо чтобы геттер пересчитывался при изменении state.posts
return (catId) => {
if (map.has(catId)) {
return map.get(catId);
}
let posts = state.posts.filter(p => p.catId === catId);
map.set(catId, posts);
return posts;
};
}
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question