const app = getApp()Page({data: {questions:{},},onLoad: function (options) {this.requestTopic(this);},onReady: function () {},onShow: function (){console.log(this.data);console.log(this.data.questions);},requestTopic : (that) =>{var params = { action: "topics"};wxApi.postRequest(app.globalData.serverUrl, params).then( (res) => { that.data.questions = res.data; //that.setData({ questions: res.data })})}})代码精简过, 在onShow中,console.log(this.data.questions);怎么就拿不到值了呢 -_!!!
2 回答

ABOUTYOU
TA贡献1812条经验 获得超5个赞
onShow在request返回前执行,所以你在onShow里面打印question的时候,肯定是没有值的。
那么为什么同样是在onShow里面打印data,data里面的question却有值呢?是因为chrome打印出来的对象并不是简单的打印它当时的值,而是打印的是该对象的引用,所以之后你对该对象的所有改变,也会反映到之前的打印结果中去。
一个简单的办法验证这一点,你在request里面打一个断点,当执行到这个断点的时候,你看看onShow里面打印的data是不是有值。然后放开断点,再看是不是有值。然后你就明白了。
添加回答
举报
0/150
提交
取消