微信小程序中wxml中动态数据无法更新?

小程序 文章 2022-02-07 10:20 1721 0 全屏看文

AI助手支持GPT4.0

微信小程序中wxml中动态数据无法更新?Can't update dynamic data in wxml in WeChat applet?

微信小程序中wxml中动态数据无法更新?

如图所示,我在这个界面一开始就设置了一个空的对象组作为班级成员列表(重点看student_list)

然后在js文件自带的onLoad函数修改了这个值

获取什么的没问题,直接看编译器是有结果的

然后我这个页面的wxml布局是这样的,里面用到了student_list这个变量

但是界面上并没有渲染出刚刚获取到的一个数据

我怀疑是wxml没有获取到最新的student_list,于是修改了student_list的初始值,如下图(虽然和之前获取的不一样,但是基本要素都具备)

果然小程序界面只渲染出初始这些东西,刚刚获取的新数据完全没覆盖上去

但是就到此为止卡住了,根本不知道怎么解决。我试过在页面初次渲染完之后再执行一次onLoad(也是在js中自带的onReady中加的),也没有把数据更新上去(后来注意到已存手机数是正常变化的,并不是我设置的初始值0,说明其他变量是正常更新的,与页面渲染与onLoad函数的执行前后顺序无关),所以怀疑是其他地方出了问题导致对象组在wxml中更新不了。


搞了快一个小时了,开放文档、开放社区、百度、B站都跑过了无果,跪求大佬解答,有什么不清楚的提出来我马上回复,谢谢大家了

As shown in the figure, at the beginning of this interface, I set an empty object group as the class member list (focus on student_list), and then modify this value in the onload function of JS file. It's no problem to get what. It depends directly on the result of the compiler. Then the wxml layout of my page is like this, in which student is used_ List is a variable, but the interface does not render the data just obtained. I suspect that wxml does not obtain the latest student_ List, so the student is modified_ The initial value of list is shown in the following figure (although it is different from the previous ones, but the basic elements are available). Sure enough, the applet interface only renders these initial things. The new data just obtained is not covered at all, but it's stuck so far. I don't know how to solve it. I tried to execute onload again after the initial rendering of the page (also added in onready built in JS), and did not update the data (later I noticed that the number of stored mobile phones changed normally, which is not the initial value 0 I set, indicating that other variables were updated normally, which has nothing to do with the sequence of page rendering and onload function execution), Therefore, it is suspected that there is something wrong in other places, which makes the object group unable to be updated in wxml. It's been almost an hour. Open documents, open communities, Baidu and station B have all run fruitlessly. I kneel down and ask the boss for an answer. If there's anything unclear, I'll reply immediately. Thank you

回答:

|G.XIAO|:

用 this.data 更新数据拿不到最新的数据。可以搞个简单的代码片段看看

what:太感谢了
dreamhunter:

需要用setData https://developers.weixin.qq.com/miniprogram/deveference/api/Page.html#%E7%BB%84%E4%BB%B6%E4%BA%8B%E4%BB%B6%E5%A4%84%E7%90%86%E5%87%BD%E6%95%B0

  1. 直接修改 this.data 而不调用 this.setData 是无法改变页面的状态的,还会造成数据不一致


what:感谢!
brave:

更新视图要用 setData

直接修改 this.data.xxx 只会影响逻辑层的值。

what:谢谢谢谢

-EOF-

AI助手支持GPT4.0