vuex(2)

vuex: dispatch commit 区别

 

dispatch:含有异步操作,例如向后台提交数据,写法: this.$store.dispatch('action方法名',值)

commit:同步操作,写法:this.$store.commit('mutations方法名',值)

 

 

直接修改state dispatchcommit来修改state的差异:

. 使用vuex修改state时,有两种方式:

1)可以直接使用 this.$store.state.变量 = xxx;
2)this.$store.dispatch(actionType, payload或者:  this.$store.commit(commitType, payload)

 

 

. 异同点

  1)共同点: 能够修改state里的变量,并且是响应式的(能触发视图更新)
  2)不同点:
      若将vue创建 store 的时候传入 strict: true, 开启严格模式,那么任何修改state的操作,只要不经过

      mutation的函数,vue就会  throw error :    [vuex] Do not mutate vuex store state outside mutation handlers

猜你喜欢

转载自www.cnblogs.com/txhy/p/11483518.html