javascript - Can I call commit from one of mutations in Vuex store -
i have vuex store, following:
import spreeapi '../../gateways/spree-api' // initial state const state = { products: [], categories: [] } // mutations const mutations = { set_products: (state, response) => { state.products = response.data.products commit('set_categories') }, set_categories: (state) => { state.categories = state.products.map(function(product) { return product.category}) } } const actions = { fetch_products: (state, filters) => { return spreeapi.get('products').then(response => state.commit('set_products', response)) } } export default { state, mutations, actions }
i want call mutation: set_categories
mutation: set_products
, gives me error:
projectfilter.js:22 uncaught (in promise) referenceerror: commit not defined(…)
what should correct way this. tried store.commit
, this.commit
, these gave similar errors.
when doing mutation, there no way commit
mutation. mutation synchronous call changes state. within 1 mutation, not able commit mutation.
here api reference vuex: https://vuex.vuejs.org/en/api.html
as can see, mutation handler receives state
, payload
, nothing more. therefore getting commit
undefined
.
in case above, can set product , categories part of same mutation handler single commit. can try if following code works:
// mutations const mutations = { set_products_and_categories: (state, response) => { state.products = response.data.products state.categories = state.products.map(function(product) { return product.category}) }, // ... }
Comments
Post a Comment