Can I call commit from one of mutations in Vuex store

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

    i think

    calling mutation from another mutation is bad idea because of hard to debug state and components

    const mutations = {
        mutationOne(state, payload){
            this.commit("mutationTwo", payload)
        },
        mutationTwo(state, payload){
            console.log("called from another mutation", payload)
        }
    }
    

    but you can write simple function and function can reusable

    function mysecondfn(state,payload){
    {
    // do your stuff here
    }
    
    
    const mutations = {
        mutationOne(state, payload){
    mysecondfn(state,payload)
         },
    
    }
    

提交回复
热议问题