美文网首页
Now you can provide attr "wx:key

Now you can provide attr "wx:key

作者: _Gaara_ | 来源:发表于2020-10-19 14:24 被阅读0次

    写小程序循环模块的语句的时候总会报一个警告,烦得很

    [WXML Runtime warning] ./pages/index/index.wxml
     Now you can provide attr `wx:key` for a `wx:for` to improve performance.
      3 |     <swiper indicator-dots="true"
      4 |       autoplay="true" interval="3000" duration="1000"  circular="true" indicator-active-color="#7fabfd">
    > 5 |       <block wx:for="{{banners}}"  >
        |        ^
      6 |         <swiper-item>
      7 |           <image src="{{item}}" class="slide-image"/>
      8 |         </swiper-item>
    

    解决办法:
    这个问题是因为没有提供当前标签的主键。
    那就好办了

      <block wx:for="{{banners}}" wx:key="*this" >
    

    解决。
    官方说法是:
    如果列表中项目的位置会动态改变或者有新的项目添加到列表中,并且希望列表中的项目保持自己的特征和状态(比如<input/>中的输入内容,<switch/>的选中状态),需要使用wx:key指定列表中项目的唯一标识符。

    相关文章

      网友评论

          本文标题:Now you can provide attr "wx:key

          本文链接:https://www.haomeiwen.com/subject/yqbkmktx.html