uni小程序onLaunch方法改为同步(vue3版本)

1,381 阅读1分钟

我遇到的问题

app.vue里的onLaunch中如果有异步方法,返回结果可能会在页面的 onLoad 之后,为了让页面的 onLoad 在 onLaunch 之后执行,可以使用以下解决方案

man.js中

// 同步onlaunched
app.config.globalProperties.$onLaunched = new Promise(resolve => {
  app.config.globalProperties.$isResolve = resolve
})

app.vue文件中

import { getCurrentInstance } from "vue"
const { proxy } = getCurrentInstance()

onLaunch: function() {
    // to do .....
    proxy.$isResolve()
}

page文件中

import { getCurrentInstance } from "vue"
const { proxy } = getCurrentInstance()

onLoad(async (option) => {
    await proxy.$onLaunched
    
    // to do .....
}

以上步骤执行完就可以拉。。。。。。。。。。。