微信小程序app.js的onLaunch执行完之后再执行Page的onLoad & 小程序onLaunch和onLoad执行顺序

需求

在微信小程序分享页面时,打开页面按理应该是先走完app.jsonLaunch onShow执行分享页面onLoadonShow;但现因为app.jsonLaunch异步操作,在等待返回值的时候Page里的onLoad事件就已经执行了。

想要的结果

App onLaunch -> Page onLoad -> App onLaunch sucess callback-> Page onLoad

具体实现案例

1、app.js

js 复制代码
//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
  }
 })

2、分享页面的page

js 复制代码
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);
         }
        }
      }
    }
  }

相关文章

基于ElementUi再次封装基础组件文档


基于ant-design-vue再次封装基础组件文档


vue3+ts基于Element-plus再次封装基础组件文档

相关推荐
ssshooter1 小时前
小程序分包页面报 "has not been registered yet" 的隐蔽根因:一个动态 import 拖垮整个分包
前端·javascript·微信小程序
JudithHuang2 天前
受够了带广告的单位换算 App,我自己做了一个微信小程序
微信小程序
小羊Yveesss2 天前
2026年如何自己搭建一个外贸网页?单页验证和完整网站的区别
微信小程序·小程序·开源
小羊Yveesss3 天前
2026年微信小程序搭建一般需要多长时间?不同方案周期和延期点
微信小程序·小程序
小羊Yveesss3 天前
2026年微信小程序搭建算什么费用?年费、设计费、开发费和维护费
微信小程序·小程序
小羊Yveesss4 天前
2026年微信小程序后端怎么搭建?后台、数据、接口和运维怎么选
运维·微信小程序·小程序
小皮虾4 天前
小程序首页性能优化实战:从 4 秒到 1.8 秒
前端·微信小程序
衍生星球6 天前
SpringBoot3 + Vue3 + 微信小程序如何学习,以及学哪些技术,组件
sql·微信小程序·uni-app·vue·springboot
新华财经频道6 天前
2026微信小程序搭建平台实测测评,优缺点解析
微信小程序·小程序
kidding7237 天前
旋转大转盘小程序
前端·css·微信小程序·小程序·前端框架