need

If there is an asynchronous method (eg: login) in onLaunch in app.vue, the returned result may be after onLoad of the page, but the method in onLoad needs the result of the login callback

The requirements are: In order to make the page's onLoad execute after onLaunch

Program

main.js

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

app.vue

The pseudo-code

onLaunch(options) {
    // #ifdef MP-WEIXIN
    let that = this;
    uni.login({
        provider: 'weixin',
        success: function(res) {
            http('user.getUserInitInfo',{code:res.code},'Loading...').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('The network is busy, please re-enter the applet');
                }
                that.$isResolve();
            });
        }
    });
    // #endif
    init(options);
}

reLaunch reloads the page

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

评论列表 共有 0 评论

暂无评论

微信服务号

微信客服

淘宝店铺

support@elephdev.com

发表
评论
Go
顶部