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

转载于:https://www.cnblogs.com/txhy/p/11483518.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值