One can create a vuex store getter which takes a parameter argument as illustrated here: https://vuex.vuejs.org/en/getters.html
I'm using Typescript (https://github.com/hmexx/vue_typescript_starter_kit) to write my code, but I can't figure out how to write a getter that takes a parameter argument. ie, the following does not seem to work:
export function getItemById(state : State, id : Number) : MyItem | undefined {
if(id === undefined) {
return undefined;
}
for(const item of state.items) {
if(item.id === id) {
return item;
}
}
return undefined;
}
export default <GetterTree<State, any>> {
getItemById
};
based on vuex docs you can implement the following:
getters: {
// ...
getTodoById: (state, getters) => (id) => {
return state.todos.find(todo => todo.id === id)
}
}
store.getters.getTodoById(2) // -> { id: 2, text: '...', done: false }
in typescript you might try something like this:
export default {
getItemById(state: State, getters: any) {
return (id: number) => {
return state.items.find(item => item.id === id);
}
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With