赞
踩
在微信小程序分享页面时,打开页面按理应该是先走完
app.js
的onLaunch
onShow
在执行分享页面
的onLoad
和onShow
;但现因为app.js
的onLaunch
是异步操作
,在等待返回值的时候Page
里的onLoad事件
就已经执行了。
[App] onLaunch -> [Page] onLoad -> [App] onLaunch sucess callback-> [Page] onLoad
//app.js App({ onLaunch () { wx.request({ url: '接口', //仅为示例,并非真实的接口地址 data: { }, success(res)=> { this.globalData.haveLoginFlag= res.data.haveLoginFlag; // 所以此处加入 callback 以防止这种情况 if (this.checkLoginReadyCallback){ this.checkLoginReadyCallback({haveLoginFlag:res.data.haveLoginFlag}); } } }) }, globalData: { haveLoginFlag: false } })
const app = getApp() onLoad() { //判断app.js onLaunch是否执行完毕 if (app.globalData.haveLoginFlag) { const { spuId, selectedAddr } = this.data; const newAddr = wx.getStorageSync('addressMsg'); if (selectedAddr.addressId != newAddr?.addressId) { this.getDetail(spuId); } } else { app.checkLoginReadyCallback = res => { if(res.haveLoginFlag){ const { spuId, selectedAddr } = this.data; const newAddr = wx.getStorageSync('addressMsg'); if (selectedAddr.addressId != newAddr?.addressId) { this.getDetail(spuId); } } } } }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。