custom-long-list.uvue 2.0 KB
Newer Older
1
<template>
2 3 4 5 6 7 8 9 10 11 12 13 14 15
  <view style="flex: 1;background-color: aliceblue;">
    <page-head :title="title"></page-head>
    <view class="tips">使用部分渲染优化列表初始元素较多时加载卡顿的问题,此示例中仅渲染滚动容器上下5屏的内容。适用于仅使用一个for循环创建所有列表项的场景。</view>
    <custom-list-view style="flex: 1;" :list="list" @scrolltoupper="scrolltoupper" @scroll="scroll">
      <template v-slot:default="{items}">
        <custom-list-item class="item" v-for="item in (items as Item[])" :item="item" :key="item.id">
          <view class="item-wrapper">
            <view class="name"><text style="font-size: 14px;">{{item.name}}</text></view>
            <view class="info"><text style="font-size: 12px; color: #999999;">{{item.info}}</text></view>
          </view>
        </custom-list-item>
      </template>
    </custom-list-view>
  </view>
16 17 18 19 20
</template>

<script>
  type Item = {
    id : number
21 22
    name : string
    info : string
23 24 25 26 27 28 29 30 31 32
  }
  import CustomListView from "./custom-list-view"
  import CustomListItem from "./custom-list-item"
  export default {
    components: {
      CustomListView,
      CustomListItem
    },
    data() {
      return {
33
        title: '自行实现长列表组件',
34 35 36 37
        list: [] as Item[]
      }
    },
    created() {
38
      for (let i = 0; i < 2000; i++) {
39 40
        this.list.push({
          id: i,
41 42
          name: `Wifi_` + i,
          info: `信号强度: -${(Math.floor(Math.random() * 60) + 40)} db, 安全性: WPA/WPA2/WPA3-Personal`
43 44 45 46 47 48 49
        } as Item)
      }
    },
    methods: {
      scrolltoupper() {
        console.log('scroll top upper')
      },
50 51
      scroll() {
        // console.log('scroll')
52 53 54 55 56 57
      }
    }
  }
</script>

<style>
58 59 60 61 62 63 64 65 66 67
  .tips {
    margin: 10px;
    border-radius: 5px;
    padding: 10px;
    background-color: white;
  }

  .item {
    padding: 5px 10px;
  }
68

69 70 71 72 73 74
  .item-wrapper {
    padding: 10px;
    border-radius: 5px;
    background-color: white;
  }
</style>