0
点赞
收藏
分享

微信扫一扫

uniapp的启动生命周期uni.onLanuch中执行的异步可能会万一onLoad生命周期注册await全局锁

小禹说财 2022-03-17 阅读 13

uniapp的启动生命周期uni.onLanuch中执行的异步可能会万一onLoad生命周期

解决方法:注册await全局锁
1、main.js添加如下代码

Vue.prototype.$onLaunched = new Promise(resolve => {
    Vue.prototype.$isResolve = resolve
})

2、在 App.vue 的 onLaunch 中增加代码 this.$isResolve()
这个方法必须在你的业务如ajax执行完毕后再执行

    async onLaunch() {
        uni.console.log("App onLaunch");
        //初始化全局埋点工具
        uni.BurialPoint = new BurialPoint();
        await onLaunch(this);
        this.$isResolve();
        //初始化远端真机调试工具
        if (config.deviceDebuggerH5) {
          // #ifndef APP-PLUS
          Debugger.startupDebugger(currentIP);
          uni.plus.screen.lockOrientation('portrait-primary')
          // #endif
        }
      },

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

async onLoad(option) {
    //等待登录成功	
    await this.$onLaunched;
	
    // 后续业务逻辑
},

https://blog.csdn.net/yfx000/article/details/108186719?utm_term=uniapp%E8%AE%BE%E7%BD%AE%E5%87%BD%E6%95%B0%E5%9C%A8%E5%85%B6%E4%BB%96%E5%87%BD%E6%95%B0%E6%89%A7%E8%A1%8C%E5%AE%8C%E5%86%8D%E6%89%A7%E8%A1%8C&utm_medium=distribute.pc_aggpage_search_result.none-task-blog-2allsobaiduweb~default-1-108186719&spm=3001.4430

举报

相关推荐

0 条评论