问题描述:
//app.js App({ globalData:{ nickname:'' }, onLaunch: function () { let that=this; //假设已经授权成功 wx.getUserInfo({ success(res){ //获取用户信息并赋值给globalData that.globalData.nickname=res.userInfo.nickName; } }) } }) //index.js const app = getApp() Page({ data: { myNickName:'' }, onl oad: function (option) { console.log(app.globalData.nickname); //结果:undefined this.setData({ myNickName:app.globalData.nickname }) //由于wx.getUserInfo()是异步请求,不知道什么时候成功返回数据,导致获取失败 } })
时序图描述: