兩者的區(qū)別
-
this.$store.commit()
commit: 同步操作
this.$store.commit('方法名',值)【存儲】
this.$store.state.方法名【取值】
-
this.$store.dispatch()
dispatch: 異步操作
this.$store.dispatch('方法名',值)【存儲】
this.$store.getters.方法名【取值】
當(dāng)操作行為中含有異步操作:
比如向后臺發(fā)送請求獲取數(shù)據(jù),就需要使用action的dispatch去完成了。
其他使用commit即可。
其他了解:commit=>mutations,用來觸發(fā)同步操作的方法。
dispatch =>actions,用來觸發(fā)異步操作的方法。
在store中注冊了mutation和action,在組件中用dispatch調(diào)用action,然后action用commit調(diào)用mutation,