this.$store.dispatch() 与 this.$store.commit()方法的区别

commit: 同步操作

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

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

dispatch: 异步操作

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

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

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

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

posted on 2020-06-10 15:45  年夜饭  阅读(7722)  评论(0编辑  收藏  举报

导航