两者的区别
(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方法
Original: https://www.cnblogs.com/rainbow70626/p/16459131.html
Author: rainbow70626
Title: Vue中this.$store.dispatch() 与 this.$store.commit()的区别
原创文章受到原创版权保护。转载请注明出处:https://www.johngo689.com/553220/
转载文章受原作者版权保护。转载请注明原作者出处!