onLoad () to load less than the value of a global variable app.globalData
app.js
globalData: {
statusResult:null,
}
index.js
wx.request({
url: 'xxx',
method: 'POST',
header: {
'content-type': 'application/json',
},
success: function (res) {
var dataInfo = res.data.data;
var name = [];
for (let i in dataInfo) {
name = dataInfo[i].Items.name
}
app.globalData.statusResult = name; //把数据存到全局变量中
}
})
device.js
data: {
statusResult:[]
},
onLoad: function () {
this.setData({
statusResult: app.globalData.statusResult
})
},
I finally print out the results, it is the first implementation decive.js in the onload (), acquired a global variable is null, then the request index.js then perform in.
I tried many methods, but finally added a loading and timers to solve this problem
onLoad: function () {
var that = this;
wx.showLoading({
title: '加载中',
})
setTimeout(function () {
that.setData({
statusResult : app.globalData.statusResult
})
wx.hideLoading()
}, 1000)
},