找回密码
 立即注册
搜索
查看: 473|回复: 0

uniapp小程序登录onLaunch和onLoad执行顺序

[复制链接]

581

主题

110

回帖

4066

积分

管理员

积分
4066

众神之神

发表于 2022-5-6 09:56:09 | 显示全部楼层 |阅读模式
1,登录放到onLaunch中,初始化完成时触发(全局只触发一次);2,因为用code换openid需要发网络请求,所以得到openid的时候,可能是在已经执行onLoad之后了;


uniapp解决方案
1、main.js添加如下代码
  1. Vue.prototype.$onLaunched = new Promise(resolve => {
  2.     Vue.prototype.$isResolve = resolve
  3. })
复制代码


2、应用启动时,设置需要等待的逻辑
在 App.vue 的 onLaunch 中增加代码 this.$isResolve()
这个方法必须在你的业务如ajax执行完毕后再执行

  1. onLaunch: function() {
  2.         console.log('App Launch');
  3.                        
  4.         console.log('倒计时10秒');
  5.         setTimeout(() => {
  6.             console.log('倒计时结束');
  7.             this.$isResolve()
  8.         },5000);
  9. },
复制代码


3、在页面 onLoad 中增加等待代码 await this.$onLaunched
注意onload要添加async,否则编译不过去

  1. async onLoad(option) {
  2.         //等待倒计时       
  3.         await this.$onLaunched;
  4.         console.log("等待倒计时结束后打印");
  5.                        
  6.         // 后续业务逻辑
  7. },
复制代码


您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

Archiver|手机版|小黑屋|十三博客 ( 鲁ICP备2023000528号 )

GMT+8, 2026-6-1 18:16 , Processed in 0.049696 second(s), 21 queries .

Powered by Discuz! X3.5

© 2001-2024 Discuz! Team.

快速回复 返回顶部 返回列表