Can I call commit from one of mutations in Vuex store

后端 未结 12 1073
醉酒成梦
醉酒成梦 2021-02-03 16:25

I have a vuex store, like following:

import spreeApi from \'../../gateways/spree-api\'
// initial state
const state = {
  products: [],
  categories: []
}

// mu         


        
12条回答
  •  孤独总比滥情好
    2021-02-03 17:27

    I prefer to call mutations.SET_CATEGORIES(state) instead of: - calling 2 different commits from an artificial action - or doing commit() inside a mutation as it makes unit testing more difficult.

    const mutations = {
     SET_PRODUCTS: (state, response) => {
       state.products = response.data.products
       mutations.SET_CATEGORIES(state)
     },
     SET_CATEGORIES: (state) => {
       state.categories = state.products.map(product => product.category)
     }
    }
    

    My opinion is that you don't need to see SET_CATEGORIES in the VueToolbox. The time travel should work anyways. Please, correct me if I'm wrong.

提交回复
热议问题