vuex中store.commit和store.dispatch的區(qū)別及使用方法
store.commit和store.dispatch的區(qū)別及使用
代碼示例:
this.$store.commit('loginStatus', 1); this.$store.dispatch('isLogin', true);
規(guī)范的使用方式
// 以載荷形式 store.commit('increment',{ ? amount: 10 ? //這是額外的參數(shù) }) // 或者使用對象風格的提交方式 store.commit({ ? type: 'increment', ? amount: 10 ? //這是額外的參數(shù) })
主要區(qū)別
dispatch
:含有異步操作,數(shù)據(jù)提交至 actions ,可用于向后臺提交數(shù)據(jù)
this.$store.dispatch('isLogin', true);
commit
:同步操作,數(shù)據(jù)提交至 mutations ,可用于讀取用戶信息寫到緩存里
this.$store.commit('loginStatus', 1);
this.$store.dispatch() 與 this.$store.commit()
傳值給vuex的mutation改變state
commit
: 同步操作
- 存儲
this.$store.commit('changeValue',name)
- 取值
this.$store.state.changeValue
dispatch
: 異步操作
- 存儲
this.$store.dispatch('getlists',name)
- 取值
this.$store.getters.getlists
總結
以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
- vuex之this.$store.dispatch()與this.$store.commit()的區(qū)別及說明
- vuex中this.$store.commit和this.$store.dispatch的基本用法實例
- vuex中store存儲store.commit和store.dispatch的區(qū)別及說明
- Vuex中this.$store.commit()和this.$store.dispatch()區(qū)別說明
- vuex中store存儲store.commit和store.dispatch的用法
- 在Vuex使用dispatch和commit來調用mutations的區(qū)別詳解
- Vue中commit和dispatch區(qū)別及用法辨析(最新)
相關文章
vue keep-alive實現(xiàn)多組件嵌套中個別組件存活不銷毀的操作
這篇文章主要介紹了vue keep-alive實現(xiàn)多組件嵌套中個別組件存活不銷毀的操作,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-10-10vue中keep-alive、activated的探討和使用詳解
這篇文章主要介紹了vue中keep-alive、activated的探討和使用詳解,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-07-07Antd-vue Table組件添加Click事件,實現(xiàn)點擊某行數(shù)據(jù)教程
這篇文章主要介紹了Antd-vue Table組件添加Click事件,實現(xiàn)點擊某行數(shù)據(jù)教程,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-11-11vue3 provide和inject底層組件的值不是響應式的處理詳解
這篇文章主要為大家介紹了vue3 provide和inject底層組件的值不是響應式的處理詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2023-08-08Vue中使用elementui與Sortable.js實現(xiàn)列表拖動排序
這篇文章主要為大家詳細介紹了Vue中使用elementui與Sortable.js實現(xiàn)列表拖動排序,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下2022-04-04vue使用Echarts設置數(shù)據(jù)無效問題記錄及解決方法
這篇文章主要介紹了vue使用Echarts設置數(shù)據(jù)無效問題記錄,本文通過場景分析給大家分享解決方法,需要的朋友可以參考下2022-08-08