globalData 上注册回调,page 里执行这个回调,这种写法网上很多
小程序加载时,index页面onload调用app.js函数,为什么会重复输出?怎么解决啊?// app.js 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:{} } }) //index.js 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) }) } } }) [图片]
01-18