Infiniteloading组件

介绍

列表滚动到底部自动加载更多数据。

安装

  1. import { createApp } from 'vue';
  2. import { InfiniteLoading } from '@nutui/nutui';
  3. const app = createApp();
  4. app.use(InfiniteLoading);

代码演示

基础用法

  1. <ul class="infiniteUl" id="scroll">
  2. <nut-infiniteloading
  3. containerId = 'scroll'
  4. :use-window='false'
  5. :has-more="hasMore"
  6. @load-more="loadMore"
  7. >
  8. <li class="infiniteLi" v-for="(item, index) in defultList" :key="index"></li>
  9. </nut-infiniteloading>
  10. </ul>
  1. setup() {
  2. const hasMore = ref(true);
  3. const data = reactive({
  4. defultList: []
  5. });
  6. const loadMore = done => {
  7. setTimeout(() => {
  8. const curLen = data.defultList.length;
  9. for (let i = curLen; i < curLen + 10; i++) {
  10. data.defultList.push(`${i}`);
  11. }
  12. if (data.defultList.length > 30) hasMore.value = false;
  13. done()
  14. }, 500);
  15. };
  16. const init = () => {
  17. for (let i = 0; i < 10; i++) {
  18. data.defultList.push(`${i}`);
  19. }
  20. }
  21. onMounted(() => {
  22. init()
  23. });
  24. return { loadMore, hasMore, ...toRefs(data) };
  25. }

下拉刷新

  1. <ul class="infiniteUl" id="refreshScroll">
  2. <nut-infiniteloading
  3. pull-icon="JD"
  4. container-id="refreshScroll"
  5. :use-window="false"
  6. :is-open-refresh="true"
  7. :has-more="refreshHasMore"
  8. @load-more="refreshLoadMore"
  9. @refresh="refresh"
  10. >
  11. <li
  12. class="infiniteLi"
  13. v-for="(item, index) in refreshList"
  14. :key="index"
  15. ></li
  16. >
  17. </nut-infiniteloading>
  18. </ul>
  1. setup() {
  2. const refreshHasMore = ref(true);
  3. const data = reactive({
  4. refreshList: []
  5. });
  6. const refreshLoadMore = done => {
  7. setTimeout(() => {
  8. const curLen = data.refreshList.length;
  9. for (let i = curLen; i < curLen + 10; i++) {
  10. data.refreshList.push(
  11. `${i}`
  12. );
  13. }
  14. if (data.refreshList.length > 30) refreshHasMore.value = false;
  15. done()
  16. }, 500);
  17. };
  18. const refresh = (done) => {
  19. setTimeout(()=>{
  20. Toast.success('刷新成功');
  21. done()
  22. },1000)
  23. }
  24. const init = () => {
  25. for (let i = 0; i < 10; i++) {
  26. data.refreshList.push(`${i}`);
  27. }
  28. }
  29. onMounted(() => {
  30. init()
  31. });
  32. return { refreshLoadMore, refreshHasMore, refresh, ...toRefs(data) };
  33. }

自定义加载文案

  1. <ul class="infiniteUl" id="customScroll">
  2. <nut-infiniteloading
  3. container-id = 'customScroll'
  4. :use-window='false'
  5. :has-more="customHasMore"
  6. @load-more="customLoadMore"
  7. >
  8. <li class="infiniteLi" v-for="(item, index) in customList" :key="index"></li>
  9. <template v-slot:loading>
  10. <div class="loading">
  11. <span>加载中...</span>
  12. </div>
  13. </template>
  14. <template v-slot:unloadMore>
  15. <div class="unload-more">没有数据啦 ~~</div>
  16. </template>
  17. </nut-infiniteloading>
  18. </ul>
  1. setup() {
  2. const customHasMore = ref(true);
  3. const data = reactive({
  4. customList: ['']
  5. });
  6. const customLoadMore = done => {
  7. setTimeout(() => {
  8. const curLen = data.customList.length;
  9. for (let i = curLen; i < curLen + 10; i++) {
  10. data.customList.push(`${i}`);
  11. }
  12. if (data.customList.length > 30) customHasMore.value = false;
  13. done()
  14. }, 500);
  15. };
  16. const init = () => {
  17. for (let i = 0; i < 10; i++) {
  18. data.customList.push(`${i}`);
  19. }
  20. }
  21. onMounted(() => {
  22. init()
  23. });
  24. return { customHasMore, customLoadMore,...toRefs(data) };
  25. }

API

Props

参数说明类型默认值
has-more是否还有更多数据Booleantrue
threshold距离底部多远加载Number200
use-window将滚动侦听器添加到 window 否则侦听组件的父节点Booleantrue
use-capture是否使用捕获模式 true 捕获 false 冒泡Booleanfalse
container-id在 useWindow 属性为 false 的时候,自定义设置节点IDString‘’
load-more-txt“没有更多数”据展示文案String‘哎呀,这里是底部了啦’
is-open-refresh是否开启下拉刷新Booleanfalse
pull-icon下拉刷新图标名称StringInfiniteLoading  滚动加载 - 图1
pull-txt下拉刷新提示文案String松手刷新
load-icon上拉加载图标名称BooleanInfiniteLoading  滚动加载 - 图2
load-txt上拉加载提示文案String加载中…

Events

事件名说明回调参数
load-more继续加载的回调函数done 函数,用于关闭加载中状态
scroll-change实时监听滚动高度滚动高度
refresh下拉刷新事件回调done 函数,用于关闭加载中状态

InfiniteLoading  滚动加载 - 图3