微信小程序 onLaunch中异步请求,在页面中获取数据延迟获取不到
在onLaunch中异步请求,获取用户状态,进而在页面中使用数据进行处理时,页面中获取数据失败
// app.js onLaunch () { // 请求 that.$u.post(that.$api.checkCodeNoToken, {code: res.code}).then(res2 => { that.globalData.checkUser = res2.checkUser; if(getApp().checkUserCallback) { // 通过getApp() 获取在page中定义的回调函数
getApp().checkUserCallback(res2.checkUser); } }); } // page.js
onLoad () {
if(getApp().globalData.checkUser && getApp().globalData.checkUser !== '') { console.log("监听appLaunch")
this.checkUser = getApp().globalData.checkUser; } else { app.checkUserCallback = checkUser => { console.log("回调", checkUser);
this.checkUser = checkUser; } }
}