在微信小程序中,onLoad 函数中请求 api 后,通过 this.setData 设置数据无效,代码如下:
onLoad: function (options) {
wx.request({
url: 'https://www.02405.com/api/v1/test',
success(res){
this.setData({
'student':res.data.data
})
}
})
}
其实这是一个典型的js问题,在上面的代码中 this 指向的是 success 函数,而不是 page。将代码做如下更改就可以了。
onLoad: function (options) {
let _this = this;
wx.request({
url: 'https://welcome.lncu.cn/api/v1/test',
success(res){
_this.setData({
'student':res.data.data
})
}
})
}