0
点赞
收藏
分享

微信扫一扫

Vue中this.$store.dispatch() 与 this.$store.commit()的区别

两者的区别

(1)this.$store.commit()
  commit: 同步操作

this.$store.commit('方法名',值)【存储】

this.$store.state.方法名【取值】

(2)this.$store.dispatch()
  dispatch: 异步操作

this.$store.dispatch('方法名',值)【存储】

this.$store.getters.方法名【取值】

  当操作行为中含有异步操作,比如向后台发送请求获取数据,就需要使用action的dispatch去完成了。其他使用commit即可。

  相关基础知识:

  commit=>mutations,用来触发同步操作的方法。
  dispatch =>actions,用来触发异步操作的方法。
  在store中注册了mutation和action,在组件中用dispatch调用action,然后action用commit调用mutation。




参考链接:​​Vue中this.$store.dispatch() 与 this.$store.commit()​​

参考链接:​​vuex里面的this.$store.dispatch方法​​



举报

相关推荐

0 条评论