zoukankan      html  css  js  c++  java
  • warning: Now you can provide attr "wx:key" for a "wx:for" to improve performance.

    小程序开发过程中在写for循环的时候会出现如下报错

    warning: Now you can provide attr "wx:key" for a "wx:for" to improve performance.
    <block wx:for="{{imgUrls}}" wx:key="swiper">
          <swiper-item>
              <image src='{{item}}' class='slide-image' width="355" height="150"></image> 
          </swiper-item>
    </block>

    小程序API文档表示:

    wx:key

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

    wx:key 的值以两种形式提供

    1. 字符串,代表在 for 循环的 array 中 item 的某个 property,该 property 的值需要是列表中唯一的字符串或数字,且不能动态改变。
    2. 保留关键字 *this 代表在 for 循环中的 item 本身,这种表示需要 item 本身是一个唯一的字符串或者数字,如:

    当数据改变触发渲染层重新渲染的时候,会校正带有 key 的组件,框架会确保他们被重新排序,而不是重新创建,以确保使组件保持自身的状态,并且提高列表渲染时的效率。

    如不提供 wx:key,会报一个 warning, 如果明确知道该列表是静态,或者不必关注其顺序,可以选择忽略。

    于是......在block上加上wx:key属性就不报错了

    <block wx:for="{{imgUrls}}">
          <swiper-item>
              <image src='{{item}}' class='slide-image' width="355" height="150"></image> 
          </swiper-item>
    </block>
  • 相关阅读:
    bzoj 3438 小M的作物
    洛谷 1126 机器人搬重物
    hdu 4055 Number String
    顺序对齐
    codevs 1300 文件排版
    NOIP 2015 提高组 Day2
    poj 2484 A Funny Game
    51nod 1548 欧姆诺姆和糖果 (制约关系优化枚举)
    hdu 1907 John (anti—Nim)
    hdu 2516 取石子游戏 (斐波那契博弈)
  • 原文地址:https://www.cnblogs.com/leiting/p/8414682.html
Copyright © 2011-2022 走看看