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()
posted @ 2022-07-08 17:30  rainbow70626  阅读(373)  评论(0编辑  收藏  举报