setData以路径更新的方式增加数组元素时,数组长度不会正确更新
- 当前 Bug 的表现(可附上截图)
数组元素增加了,数组长度没变。
- 预期表现
数组长度正确变化
- 复现路径
https://github.com/NervJS/taro/issues/882#issuecomment-440127031
- 提供一个最简复现 Demo
<!-- index.wxml --> < block > < view class = "container" > < text wx:for = "{{contents}}" wx:for-item = "item" >{{item.text}}</ text > < button bindtap = "add" >add line by path</ button > < button bindtap = "add2" >add line by replace array</ button > < button bindtap = "log" data-content = "{{contents}}" data-content-length = "{{contents.length}}" >show log</ button > < button disabled = "{{contents.length ? false : true}}" >{{contents.length}}</ button > </ view > </ block > |
let i = 0 Page({ data: { contents: [], }, add () { this .setData({ [`contents[${i++}]`]: { text: 'test' } }) }, add2() { this .setData({ contents: [... this .data.contents, { text: 'test' }] }) }, log (ev) { console.log({ contentLength: ev.currentTarget.dataset.contentLength, content: ev.currentTarget.dataset.content }) } }) |