a.vue中有個(gè)http請(qǐng)求,響應(yīng)之后this.$store.commit('userInfo', userInfo);,在b.vue頁(yè)面通過(guò)computed獲取更新之后的數(shù)據(jù)userInfo(){ return this.$store.state.userInfo
},b.vue頁(yè)面中通是有個(gè)方法需要獲取此數(shù)據(jù):this.userInfo,當(dāng)頁(yè)面刷新的刷新的時(shí)候,a.vue頁(yè)面中的頁(yè)面請(qǐng)求可能還沒(méi)有完成,b.vue 頁(yè)面就要獲取 數(shù)據(jù)了,導(dǎo)致出現(xiàn)undefined問(wèn)題;請(qǐng)問(wèn)這個(gè)問(wèn)題應(yīng)該怎么解決???
2 回答

梵蒂岡之花
TA貢獻(xiàn)1900條經(jīng)驗(yàn) 獲得超5個(gè)贊
不用想這么復(fù)雜,沒(méi)有獲取到this.userInfo
前加個(gè)loading狀態(tài),比如通過(guò)其中一個(gè)存在參數(shù)判斷
computed: { isLoading(){ return this.userInfo.token ? true : false; } }, watch: { isLoading: { handler: function(val){ if(val){// 已獲取 console.log("start...") } }, immediate: true } }
等獲取到再執(zhí)行你的代碼

qq_笑_17
TA貢獻(xiàn)1818條經(jīng)驗(yàn) 獲得超7個(gè)贊
1:等接口返回后再作跳轉(zhuǎn)
2:給this.userInfo設(shè)置默認(rèn)值,避免undefined,在組件模板進(jìn)行判斷,如果未返回就不渲染
添加回答
舉報(bào)
0/150
提交
取消