app.js页面 App({ globalData: { userInfo: "", js_code: "", openid: "", session_key: "" }, getUserInfo: function (cb) { var that = this; if (that.globalData.userInfo) { typeof cb == "function" && cb(that.globalData.userInfo) } else { wx.login({ success: res => { that.globalData.js_code = res.code wx.getUserInfo({ success: res => { that.globalData.userInfo = res.userInfo; typeof cb == "function" && cb(that.globalData.userInfo); // 请求官方接口,获取openid和session_key wx.request({ url: "https://api.weixin.qq.com/sns/jscode2session", data: { appid: "********", secret: "*******", js_code: that.globalData.js_code, grant_type: "authorization_code" }, success: function (res) { that.globalData.openid = res.data.openid; that.globalData.session_key = res.data.session_key; }, fail: function () { } }) } }) } }) } } }) index.js页面 Page({ data: { userInfos:{} }, onLoad: function (options) { var that = this; app.getUserInfo(); console.log(app.globalData); } })
在index中打印app.globalData结果是下图,而直接调用全局变量会得到初始值"",应该怎么解决?怎么获取下面的数据?
我之前这么写过,没有用,得到的是app.js里设置的初始值""
是不是在index页面没有更新数据?
var app=getApp()
onload:function(option){
var that=this
app.getUserInfo(function(userInfo)){
that.setData({
userInfo:userInfo
})
})
}