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

瀟瀟雨雨
TA貢獻1833條經(jīng)驗 獲得超4個贊
不用想這么復雜,沒有獲取到this.userInfo
前加個loading狀態(tài),比如通過其中一個存在參數(shù)判斷
computed: { isLoading(){ return this.userInfo.token ? true : false; } }, watch: { isLoading: { handler: function(val){ if(val){// 已獲取 console.log("start...") } }, immediate: true } }
等獲取到再執(zhí)行你的代碼
添加回答
舉報
0/150
提交
取消