微信小程序-wx:key的作用为什么不能使用index

wxml中的代码为
<view class="swiperContent">
    <swiper 	indicator-dots="true" autoplay="true">
      <swiper-item wx:for="{{bannerList}}" wx:key="bannerId">
        <image src="{{item.pic}}"></image>
      </swiper-item>
    </swiper>
  </view>

假如 bannerList中 wx:key 使用index 

 使用index的话,在比对的时候,系统会察觉到index为1的这个数据值变了,会更新,index为2,3,4的数据值也变了,系统消耗内存多。

使用id的话,只会察觉到 f 的id 变了。其他的id的值是没有变化的。

posted @ 2024-01-23 12:03  小小——开心  阅读(70)  评论(0编辑  收藏  举报