首页>技术积累>详细

uniapp 如何让页面的 onLoad 在 onLaunch 之后执行

2025-01-14 12:00:00 来源:https://cl

扫一扫手机浏览

微信分享

1. main.js 添加代码


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

   

    Vue.prototype.$isResolve = resolve;})

2. 在 App.vue 的 onLaunch 中添加代码 this.$isResolve()


onLaunch: function() {   // 登录 

uni.login({  

 provider: 'weixin',

 success: (loginRes) => { 

  this.$axios({   url: '', params: {   code: loginRes.code } }).then(res => { 

  try {   uni.setStorageSync('token', res.data.token);

 this.$isResolve(); }

 catch (e) {   console.error(e) } })

 } });}


3. 在页面 onLoad 中添加代码 await this.$onLaunched


async onLoad() { 

    // 等待登录结果返回

    await this.$onLaunched;

    // 处理后续业务逻辑(此时已存在token值)

    console.log('0000');

},


我们为您提供如下服务:

猜你喜欢

网站建设公司,就选我们

010-59567245/13439526847

010-59567245 / 13439526847
北京市房山区长阳镇怡和北路5号院(熙悦广场)
542722164

北京网站建设公司 模板网站

微信公众号

手机网站

© 2019-2025 All rights reserved. 北京中泽瑞科技有限公司京ICP备19054999号-1
微信:13439526847