1、main.js添加如下代码
Vue.prototype.$onLaunched = new Promise(resolve => {
Vue.prototype.$isResolve = resolve
})
2、在 App.vue 的 onLaunch
中增加代码 this.$isResolve()
这个方法必须在你的业务执行完毕后再执行,如小程序默认登录。
app.js文件
onLaunch: function() {
console.log('App Launch')
this.defaultLogin();
},
methods: {
// 小程序端默认登录
defaultLogin() {
var _this = this;
uni.login({
success: function(loginRes) {
console.log(loginRes);
_this.$u.post('登录接口', {
code: loginRes.code,
}).then(res => {
console.log('==小程序默认登录==');
console.log(res);
// 缓存token
uni.setStorageSync('token', res.data.token);
_this.$isResolve();
}).catch(res => {
console.log(res);
})
}
});
},
}
3、在页面 onLoad
中增加代码 await this.$onLaunched
注意onload要添加async,否则编译不过去。
async onLoad(option) {
//等待登录成功
await this.$onLaunched;
// 后续业务逻辑
},