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;
}
Post comment 取消回复