列表渲染

//page.js
Page({
  data: {
        message: "hello 小程序",
        list: ["html","Css","js"],
        userList: [
            {username: 'zmt', age: 35},
            {username: 'tomcat', age: 20},
            {username: 'jack', age: 30},
        ],
  }
})
<view>
    <!-- 使用wx:for遍历数组 
        index: 就是数组元素的下标
        item:  就表示数组的元素
    -->
  <view wx:for="{{list}}">
    {{index}} {{item}}
  </view>
  <!--
    自定义元素的名称,和下标的名称
   -->
   <view wx:for="{{list}}" 
    wx:for-item="ele"
    wx:for-index="eleIndex"
   >
     {{eleIndex}}  {{ele}}
   </view>

</view>

遍历对象数组

//page.js
Page({
  data: {
        userList: [
            {username: 'zmt', age: 35},
            {username: 'tomcat', age: 20},
            {username: 'jack', age: 30},
        ],
  }
})
<view>
    <!-- 
      展示用户数组
    -->
    <view wx:for="{{userList}}">
      {{index}} 

      用户名: {{item.username}}
      年龄:  {{item.age}}
    </view>
</view>

results matching ""

    No results matching ""