vuex difference between the mutation and the action and use

utations similar event for the state to submit the state Vuex

action and mutations is also very similar, the main difference is that mutations can be synchronous operation ,, action may comprise an asynchronous operation, and action may be submitted by mutations

mutations state has an internal parameter, the received state is the object of Vuex

action also has a context-specific parameter, it is state of the parent context, comprising state, getters

Vuex warehouse is store.js, will axios introduced, and add new methods in action

Distribution calling action:

this. $ store.dispatch ( 'action in the function name', data is transmitted to the action)

Mutation submitted in the assembly:
. Store.commit the this $ ( "mutation function", the data is sent to a mutation in)

Submit mutation in the action:

const store = new Vuex.Store({
  state: {
    count: 0
  },
  mutations: {
    increment (state) {
      state.count++
    }
  },
  actions: {
    increment (context) {    //官方给出的指定对象, 此处context可以理解为store对象
      context.commit('increment');
    }
  }
})
// 第一种写法简写形式
  const actions = {
    action的函数名({commit}) { 
      commit(“mutation函数名”, value);   //调用mutation中的函数
      //此处value可以是对象,可以是固定值等
    }
  }
  // 第二种形式
  const actions = {
    action中的函数名(context) {
      //context 官方给出的指定对象, 此处context可以理解为store对象
      context.commit(“mutation函数名”, value);     //调用mutation中的函数
    }
  }

Guess you like

Origin www.cnblogs.com/panghu123/p/11747285.html