when Mini Program was first developed, he thought at first that onload, was not after onlaunch had been executed, and then the necessary condition judgment was always the approximate code segment of false, first:
app.js
onLaunch: function () {
//
this.userLogin()
},
userLogin: function () {
//
wx.login({
success: res => {
// res.code openId, sessionKey, unionId
if (res.code){
wx.request({
url: "login.php",
data: {
code : res.code
},
method : "POST",
header : {
"content-type": "application/x-www-form-urlencoded"
},
success: function (res) {
getApp().globalData.hasAuthorize = true;
},
fail: function () {
console.log("!")
},
})
} else {
console.log("!" + res.errMsg)
}
}
})
},
globalData: {
userInfo: null,
hasAuthorize: false
}
index.js
onLoad: function () {
//
this.setData({
hasAuthorize: app.globalData.hasAuthorize
})
},
my question:
very often, when you open Mini Program, the onLoad of index.js has already been executed before the onLaunch is finished. As a result, hasAuthorize gets the initial value of app.globalData.hasAuthorize (false)
every time.is there any way to wait for onLaunch to finish in onLoad? Beginners ask for advice, thank you.