element-plus/website/docs/zh-CN/infiniteScroll.md
Xing.Wu 797b978f76
feat(docs): InfiniteScroll code for composition (#2193)
Co-authored-by: 无星 <32910694@qq.com>
2021-06-10 00:23:50 +08:00

2.9 KiB
Raw Blame History

InfiniteScroll 无限滚动

滚动至底部时,加载更多数据。

基础用法

在要实现滚动加载的列表上上添加v-infinite-scroll,并赋值相应的加载方法,可实现滚动到底部时自动执行加载方法。 :::demo

<template>
  <ul class="infinite-list" v-infinite-scroll="load" style="overflow:auto">
    <li v-for="i in count" class="infinite-list-item">{{ i }}</li>
  </ul>
</template>

<script>
  export default {
    data () {
      return {
        count: 0
      }
    },
    methods: {
      load () {
        this.count += 2
      }
    }
  }
</script>
<!--
<setup>

  import { defineComponent, ref } from 'vue';

  export default defineComponent({
    setup() {
      const count = ref(0);
      const load = () => {
        count.value += 2;
      };
      return {
        count,
        load,
      };
    },
  });

</setup>
-->

:::

禁用加载

:::demo

<template>
  <div class="infinite-list-wrapper" style="overflow:auto">
    <ul
      class="list"
      v-infinite-scroll="load"
      infinite-scroll-disabled="disabled">
      <li v-for="i in count" class="list-item">{{ i }}</li>
    </ul>
    <p v-if="loading">加载中...</p>
    <p v-if="noMore">没有更多了</p>
  </div>
</template>

<script>
  export default {
    data () {
      return {
        count: 10,
        loading: false
      }
    },
    computed: {
      noMore () {
        return this.count >= 20
      },
      disabled () {
        return this.loading || this.noMore
      }
    },
    methods: {
      load () {
        this.loading = true
        setTimeout(() => {
          this.count += 2
          this.loading = false
        }, 2000)
      }
    }
  }
</script>
<!--
<setup>

  import { defineComponent, ref, computed } from 'vue';

  export default defineComponent({
    setup() {
      const count = ref(10);
      const loading = ref(false);
      const noMore = computed(() => count.value >= 20);
      const disabled = computed(() => loading.value || noMore.value);
      const load = () => {
        loading.value = true;
        setTimeout(() => {
          count.value += 2;
          loading.value = false;
        }, 2000);
      };
      return {
        count,
        loading,
        noMore,
        disabled,
        load,
      };
    },
  });

</setup>
-->

:::

Attributes

参数 说明 类型 可选值 默认值
infinite-scroll-disabled 是否禁用 boolean - false
infinite-scroll-delay 节流时延单位为ms number - 200
infinite-scroll-distance 触发加载的距离阈值单位为px number - 0
infinite-scroll-immediate 是否立即执行加载方法,以防初始状态下内容无法撑满容器。 boolean - true