state 全局值 設置 和獲取


 

commit

dispatch

在store js文件中定義

state: {
shopId: ""
},
 
mutations: {
// shopid
SETID (state, data) {
console.log("app-mutation", data)
state.oldshopId = state.shopId
state.shopId = data   // 獲取傳來的值
}
},

.//設置   

let str = JSON.stringify(this.newShop);
localStorage.setItem('shop',str)    ---將值保存在緩存中
this.$store.dispatch('getParamSync',JSON.parse(localStorage.getItem('shop')))
this.$store.commit(' SETID',JSON.parse(localStorage.getItem('shop')).shopId)

 

//獲取   --- 注意得在HTML中顯示變量

computed: {
...mapState({
themeColor: state => state.app.themeColor,
menuColor: state =>
{
console.log('nav', state)
return state.app.menuColor
},
collapse: state =>{
return state.app.collapse
},
shopId: state => {   //shopId  自定義
return state.app.shopId
},
}),
 
},
、、、、、、、轉載、、、、、、

this.$store.commit('toShowLoginDialog', true);
this.$store.dispatch('toShowLoginDialog',false)
 

主要區別是:

dispatch:含有異步操作,例如向后台提交數據,寫法: this.$store.dispatch('mutations方法名',值)

commit:同步操作,寫法:this.$store.commit('mutations方法名',值)
---------------------
作者:qq_36165747
來源:CSDN
原文:https://blog.csdn.net/qq_36165747/article/details/81082963
版權聲明:本文為博主原創文章,轉載請附上博文鏈接!


免責聲明!

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



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