I have a vuex store, like following:
import spreeApi from \'../../gateways/spree-api\'
// initial state
const state = {
products: [],
categories: []
}
// mu
To share code between mutations, you must create a new function that performs the work, which you can then reuse. Fortunately, mutations are just plain old functions, and we can pass the state
parameter around however we like, so this is quite easy to do.
For example:
const mutations = {
SET_PRODUCTS: (state, response) => {
state.products = response.data.products
setCategories(state)
},
SET_CATEGORIES: (state) => {
setCategories(state)
}
}
function setCategories(state) {
state.categories = state.products.map(product => product.category)
}