数据:三层嵌套
index.wxml 页面
!-- 菜单标签 --view class='menu_label' view class='label_ul' view wx:for="{{ menuList }}" wx:key='' class="{{ label_index == index ? 'label_lis' : 'label_li' }}" bindtap='menuList' id="{{ index }}"{{ item.title }}/view /view/view!-- 图书内容 --view class='book_sec' view class='book_ul' wx:for="{{ bookUl }}" wx:key='' wx:for-item="secUl" wx:if="{{ label_index == index }}" id="{{ index }}" view class='book_li' wx:for='{{ secUl.bookSec }}' wx:key='' wx:for-item="secLi" view class='book_ol' view class='book_ol_li' wx:for='{{ secLi.bookList }}' wx:key='' wx:for-item="item" view class='book_free b'免费阅读/view image src='{{ item.img }}'/image view class='book_people b'100006人在读/view /view /view /view /view/viewindex.js页面
Page({ /** * 页面的初始数据 */ data: { menuList: [ { title: '全部' }, { title: '文学' }, { title: '技术类' }, { title: '社会科学' }, { title: '设计' }, { title: '技术类' }, { title: '社会科学' }, ], label_index: 0, bookUl: [ { bookSec: [ { bookList: [ { img: '../../img/img1_icon.png' }, { img: '../../img/img2_icon.png' }, { img: '../../img/img3_icon.png' }, ] }, { bookList: [ { img: '../../img/img4_icon.png' }, { img: '../../img/img5_icon.png' }, ] } ] }, { bookSec: [ { bookList: [ { img: '../../img/img6_icon.png' }, { img: '../../img/img5_icon.png' }, { img: '../../img/img4_icon.png' } ] }, { bookList: [ { img: '../../img/img7_icon.png' } ] } ] }, { bookSec: [ { bookList: [ { img: '../../img/img7_icon.png' }, { img: '../../img/img2_icon.png' }, { img: '../../img/img1_icon.png' }, ] }, { bookList: [ { img: '../../img/img3_icon.png' }, { img: '../../img/img4_icon.png' }, { img: '../../img/img6_icon.png' } ] } ] }, ] /** * 菜单点击切换内容 */ menuList: function (e) { let that = this; // console.log(e); var id = e.target.id; that.setData({ label_index: id }) } }, })如果列表中项目的位置会动态改变或者有新的项目添加到列表中,并且希望列表中的项目保持自己的特征和状态(如 input/ 中的输入内容,switch/ 的选中状态),需要使用 wx:key 来指定列表中项目的唯一的标识符。
如不提供 wx:key,会报一个 warning, 如果明确知道该列表是静态,或者不必关注其顺序,可以选择忽略。
wx:key 的值以两种形式提供
1)字符串,代表在 for 循环的 array 中 item 的某个 property,该 property 的值需要是列表中唯一的字符串或数字,且不能动态改变。
2)保留关键字 *this 代表在 for 循环中的 item 本身,这种表示需要 item 本身是一个唯一的字符串或者数字,如:
当数据改变触发渲染层重新渲染的时候,会校正带有 key 的组件,框架会确保他们被重新排序,而不是重新创建,以确保使组件保持自身的状态,并且提高列表渲染时的效率。
最后效果图














