没key的问题
当有3个按钮比如
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/2330cba81613ab03d54432cc321a3c5e.png)
代码
{{item}} 增加节点 data: { switchData:['a','b','c'] }, 加给按钮改变数组 this.data.switchData.push('d'); this.setData( { switchData: this.data.switchData } ); 打开前面的按钮 ![在这里插入图片描述](https://img-blog.csdnimg.cn/20200512202454934.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3FxXzQyOTA4MjU5,size_16,color_FFFFFF,t_70) 在点击增加 ![在这里插入图片描述](https://img-blog.csdnimg.cn/20200512202521626.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3FxXzQyOTA4MjU5,size_16,color_FFFFFF,t_70) 从数组尾部增加没有问题 当像数组前增加 this.data.switchData.unshift('d'); this.setData( { switchData: this.data.switchData }
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/72a5d6bd1fc633a108183a9304f9e5fe.png)
就会出现d选中现像。
为什么呢?
小程序也是diff算法会复用 先前的节点默认没有数组key的话是a:0b:1c:2
增加了d这个节点变成了虚拟dom里面变成了d:0,a:1,b,1c:2;根据diff算法是逐层比较的
现在olddom里面a:0b:1c:2变成了d;0,a:1,b:2,在创建c:2;diff结束所以出现问题,我们可以设置为一的key