Can I call commit from one of mutations in Vuex store

后端 未结 12 1074
醉酒成梦
醉酒成梦 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:21

    In your case you should consider having only one mutation, namely SET_PRODUCTS.

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

    You should never have any need to call SET_CATEGORIES separately. Think about it! Categories can only mutate if products are changed. And products can change only through SET_PRODUCTS.

提交回复
热议问题