First of all, I have to complain about the array update of the applet, and directly replace the entire array through setData
which only works on developer tools, and completely fails when it is put on the real machine. This is a very painful thing , I don't know what the update mechanism of the official team of the applet is, and I didn't take a closer look. I guess it may be related to the reference type.
Looking at the Internet, most of them specify to modify the value of an element in the array object, such as this:
Page({
data: {
list: [{name: 'tom'}]
},
onLoad() {
this.setData({[`list[0].name`]: 'jack'})
}
})
PS: I feel that it is very troublesome to deal with, and it needs to be traversed one by one, and the length of the new array and the old array may not be the same.
Because what I want to do is a simple page, only a map and a list, so I find another way to deal with it through redirectTo
, which is to update the view by refreshing the page, because the applet does not directly refresh the page API, so I thought about implementing it indirectly through reLaunch
or redirectTo
, but reLaunch
no effect, so I determined redirectTo
adding parameters The implemented view update, speaking of this, in fact, this has nothing to do with the array update, but this is indeed a bloody case caused by the ineffectiveness of the array update, and can only be saved by other means, the corresponding sample code:
Page({
data: {
activeTab: '',
list: []
},
onLoad(options) {
if(options.tab) {
this.setData({activeTab: options.tab})
}
this.getList()
},
changeTab(e) {
wx.redirectTo({url: `/pages/index/index?tab=${e.currentTarget.dataset.tab}`})
},
getList() {
wx.request({
url: 'xxx',
data: {
tab: this.data.activeTab
},
success: res => {
this.setData({list: res.data})
}
})
}
})
In the end, although the side of the problem is solved, this is not a formal solution, and there are scene limitations, so I still hope that the official can see if the update mechanism of the array can be optimized.
For more front-end knowledge, please pay attention to the applet, there will be surprises from time to time!
**粗体** _斜体_ [链接](http://example.com) `代码` - 列表 > 引用
。你还可以使用@
来通知其他用户。