欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

vuex中store.commit和store.dispatch的區(qū)別及使用方法

 更新時間:2023年01月10日 10:12:49   作者:longzhoufeng  
這篇文章主要介紹了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)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。

相關文章

最新評論