正如问题一样,我在onlaunch上写登录请求,在后台取页面上的很多公共数据,写在全局数据中,然后在子页面加载完成时使用这些数据呈现一些页面但是,在onlaunch上的请求是异步的,也就是说,在onlaunch运行的同时,onload也已经在运行,它与页面相连,下面贴上我的代码
//app.js中
onLaunch: function () {
uni.login({
提供商: ' weixin ',
success:(loginRES )={
var code=loginRes.code
uni.request({ (
url:'https://www.ceshi1.com ',
data : {代码},
方法: '开机自检',
success:(RES )={
this.global data.token=RES.data.token
}
() )
}
);
(,
//index.js中
onLoad ()。
var token=getApp ().globalData.token
uni.request({ (
URL : ' https://www.ceshi1.com/get things ',
data:{token},
方法: '开机自检',
success:(RES )={
this.global data.token=RES.data.token
}
() )
(,
正如问题所示,即微信小程序注册的接口通过接口获取token,而索引页面使用token获取页面数据。 在此再插入一个题外话,如果微信小程序使用onlaunch中的方法编写同步的内容,或者使用例如async/await将所有请求设置为同步,则onload不会等待OnLait