需求

app.vue里的 onLaunch 中如果有异步方法(比如:登录),返回结果可能会在页面的 onLoad 之后,但 onLoad 中的方法需要登录回调的结果

需求为:为了让页面的 onLoad 在 onLaunch 之后执行

方案

main.js

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

app.vue

伪代码

onLaunch(options) {
    // #ifdef MP-WEIXIN
    let that = this;
    uni.login({
        provider: 'weixin',
        success: function(res) {
            http('user.getUserInitInfo',{code:res.code},'加载中...').then(res => {
                if(res.code == 1) {
                    uni.setStorageSync('openid', res.data.openid);
                    uni.setStorageSync('user_info', res.data.user_info);
                    uni.setStorageSync('userInfo', res.data.user_info);
                    if(res.data.token) {
                        uni.setStorageSync('token', res.data.token);
                        uni.setStorageSync('isLogin', true);
                        store.commit('isLogin',true);
                    }
                } else {
                    that.$u.toast('网络繁忙,请重新进入小程序');
                }
                that.$isResolve();
            });
        }
    });
    // #endif
    init(options);
}

reLaunch重载页面

async onLoad(options) {
    await this.$onLaunched;
}
点赞(0)

评论列表 共有 0 评论

暂无评论

微信服务号

微信客服

淘宝店铺

support@elephdev.com

发表
评论
Go
顶部