为什么我js文件中的data赋值异常?

小程序 文章 2020-07-31 18:41 684 0 全屏看文

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 贴一下 代码片段

Hall of fame:问题解决了,我按网上教程写了一个文件通过编写一个能够在wxml就直接处理数据为保留两位小数的的wxs模块,这是教程,看完之后你可能会对我的问题有更清晰的认识https://www.meiwen.com.cn/subject/hypgwxtx.html
Distance:

for循环打印一下num看看是不是本身就是null

Hall of fame:问题解决了,我按网上教程写了一个文件通过编写一个能够在wxml就直接处理数据为保留两位小数的的wxs模块,这是教程,看完之后你可能会对我的问题有更清晰的认识https://www.meiwen.com.cn/subject/hypgwxtx.html
Hall of fame:

现在似乎找到原因了,可能是success回调函数中for循环的遍历出现了问题,因为每刷新4次就正常显示一次,很可能是for循环中percent与其对应数组没有对应赋值上,导致无法正常在前端按正常顺序遍历打印percent,但我想问问,我想在前端显示,realtime和capacity字段的比率怎么做,以前我是用{{realtime/capacity}}这样的形式在前端显示,但后来发现这样做很容易出现很长一段小数,所以,就像先在js内部处理成保留两位小数的模式,然后再在前端显示,但却出现了循环遍历顺序有误这样的问题,我想有其他办法让realtime和capacity的比率直接在wxml页面就可以处理为保留两位小数的模式吗

-EOF-

AI助手支持GPT4.0


国内超级便宜服务器

摸鱼人热门新闻聚合

钻级赞助商 我要加入

开发者在线工具

第三方支付技术请加QQ群

相关文章
请问getuserprofile怎么写入云数据库?
IOS10X版本报错 游戏加载失败
我们开发遇到的问题?
草稿箱里的草稿不见了怎么找回来?
组件引用微信开发者工具电脑端可以编译成功,真机调试后提示找不到路径,什么问题?
随便看看
为什么我的数组总是乱序? 481
macOS升级12.3.1 之后企业微信一直闪退 1734
在网上找到了一个openid的获取方法,但是最后的传参不会写,有大佬可以帮帮吗? 383
小程序类型为IT科技,软件服务提供商,如何开通小程序直播 ?,没有支付记录 374
试用转正后的小程序,登录公众号后台后,为什么不能修改小程序基本信息? 344
微信云托管的mysql数据库为什么在小程序中不能用内网IP连接? 1089
微信小程序进行的银行存款有保障吗? 1727
请问苹果手机小程序为什么不能添加到桌面? 638
小程序名称注册显示被占用如何根据同名账户的名称及原始ID查询同名账户的注册时间或者主体信息? 579
调用"getPhoneNumber"接口失败? 1820