vuex中store.commit和store.dispatch的區別及用法


this.$store.commit('loginStatus', 1);

this.$store.dispatch('isLogin', true);

規范的使用方式:

// 以載荷形式
store.commit('increment',{
amount: 10 //這是額外的參數
})

// 或者使用對象風格的提交方式
store.commit({
type: 'increment',
amount: 10 //這是額外的參數
})

主要區別:
dispatch:含有異步操作,數據提交至 actions ,可用於向后台提交數據

this.$store.dispatch('isLogin', true);

commit:同步操作,數據提交至 mutations ,可用於讀取用戶信息寫到緩存里

this.$store.commit('loginStatus', 1);


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM