wx:for中的数组使用unshift,自定义组件的lifetimes出现bug
自定义组件中:
lifetimes: { // 生命周期函数,可以为函数,或一个在methods段中定义的方法名 attached: function () { console.log(this.properties.name) },}, |
wxml中
<shop-item wx:for="{{arr}}" wx:key="item" name="{{item.name}}"></shop-item> |
js中是数据
arr: [ { id: 1, name: 11, price: 111, num: 1111 }, { id: 21, name: 211, price: 2111, num: 21111 }, ] |
页面一开始会log出来 11, 和 211
这个时候
在js里面输入
var l = this.data.arr; l.unshift({ id: 3, name: 3, price: 33, num: 3 }) this.setData({ arr: l }) |
预期结果是log出 一个 3, 但是 它却log出来211, 也就是没有让新加的元素初始化,而是让数组的最后一个又初始化了一遍
