AI助手支持GPT4.0
为什么我js文件中的data赋值异常?
先放出相关JS代码和wxml代码js代码onLoad: function (options) { var that=this; wx.request({ url: 'http://81.68.72.54/index/Perso/person3', method: 'POST', success: function(res) { console.log(res) for (let i = 0; i < res.data.length; i++) { var num=res.data[i].realtimees.data[i].capacity*100 num=num.toFixed(1) res.data[i].percent=num console.log(res.data[i]) that.setData({ array:res.data }) console.log(that.data.array) } } })}wxml代码 <view style="color:red;float: left;">当前人数:{{item.percent}}</view>z在循环结束后,我们打印第三行注释的结果可以看出,已经成功赋值
但是在前端却一直显示null
同时调试器中的AppData也一直显示为null
调试器的console的打印结果显示,明明that.data.array(也就是data 中的array数组)已经被更新,但前端却显示为null,而且之前也只成功显示过一次正常的数字,就是上上个截图对应位置的51 13,这是为什么
First release the relevant JS code and wxml code. JS code onload: function (options) {var that = this wx.request ({ url: ' http://81.68.72.54/index/Perso/person3 ', method: 'POST', success: function(res) { console.log (RES) / / the first line comments for (let I = 0; I < 0 res.data.length ; i++) { var num= res.data [i].realtime/ res.data [i].capacity*100 num= num.toFixed (1) res.data [i].percent=num console.log ( res.data [i])// Comment on the second line that.setData ({ array:res.data }) console.log ( that.data.array )//The third line comments}}}} wxml code < view style=“ color:red;float : left; "> current number of people:{{ item.percent }}< / View > Z at the end of the loop, we print the result of the third line of comment. We can see that the value has been assigned successfully, but null is always displayed in the front end. At the same time, appdata in the debugger is always displayed as null. The print result of the debugger's console shows that that.data.array (data The array in the previous screenshot has been updated, but the front end is displayed as null, and the normal number has only been successfully displayed once before, that is, 5113 at the corresponding position in the previous screenshot. Why is this
for循环之后 settimeout延时1秒 打印一下 array看看
你最好用开发工具写一个demo 贴一下 代码片段