App({
onLaunch() {
var that=this
that.getuserinfos().then(res=>{
that.globalData.userinfo=res.data
return that.getimg()
}).then(res=>{
that.globalData.imginfo=res
}).catch(err=>{
wx.setStorage({
key:"userinfo",
data:{"name":"你好","age":20}
})
})
},
getuserinfos(){
return new Promise((resolve,reject)=>{
wx.getStorage({
key:"userinfo",
success(res){
resolve(res)
},fail(err){
reject(err)
}
})
})
},
getimg(){
var that=this
return new Promise((resolve,reject)=>{
wx.request({
url: 'https://www.jianshu.com/p/912dc496d175',
success(res){
console.log(0)
resolve(res)
},
fail(err){
reject(err)
}
})
})
},
globalData: {
userinfo: {},
imginfo:{}
}
})
const app = getApp()
Page({
data: {
},
onLoad() {
if (Object.keys(app.globalData.imginfo).length == 0 && Object.keys(app.globalData.userinfo).length == 0) {
console.log(app.globalData.imginfo, app.globalData.userinfo)
} else if (Object.keys(app.globalData.imginfo).length == 0) {
app.getimg().then(res => {
console.log(app.globalData.imginfo)
})
}
}
})
globalData 上注册回调,page 里执行这个回调,这种写法网上很多
自己执行了2次不是很正常?
第一次:
第二次:
试试看删掉一个是不是就正常了。