App.js
onRequest:function(){
var that = this
wx.requst({
url:"xxxx",
success:function(res){
that.globalData.data = res.data
}
})
}
index.js
const app = getApp()
Page({
data: {},
onShow: function () {
app.onRequest()
console.log(app.globalData.data) // 返回的是underfind , 请问是为什么?
},
})

楼上回复正确,异步请求是有时间延迟,在你进入index onshow 触发 app.onRequest()还没成功返回时,就执行到了 console.log(app.globalData.data) 自然没有数据,你可以写一个回调, app.onRequest()执行成功后进行回调处理接下来的业务
异步。打印时app.onRequest还没有返回
App.jsonRequest:function(cb){varthat =thiswx.requst({url:"xxxx",success:function(res){that.globalData.data = res.dataif(cb)cb();}})}index.jsconst app = getApp()Page({data: {},onShow:function() {app.onRequest(function(){console.log(app.globalData.data)})},})