vant list 分页问题

本文记录了一个Vue项目中使用Vant组件库的list组件进行分页加载数据的过程,包括如何处理数据加载、空数据展示及接口调用。通过自定义组件和接口调用来实现列表的动态加载,当数据为空时显示自定义空数据页面。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

vant list 分页问题是项目中基本都会遇到的,但是还是记录一下吧,万一哪天脑袋短路不会写了怎么办(略略略~~)

	// list 是vant按需引入的组件 
	// demand-accept-list 是自定义列表组件(分页写到组件外面)
	// null-data  自定义空数据页面(有需要的自己画界面吧)
	<list   
      v-model="loading"
      :finished="finished"
      :finished-text="showEmpty ? '' : '没有更多了'"
      :immediate-check="false"
      @load="onLoad"
    >
      <demand-accept-list :list="acceptList" :tab-index="tabIndex" />
    </list>

    <null-data v-if="showEmpty" msg="暂无信息" />
 // 这是第一种方式,有点多,可以试试第二种
   selectDemandMyList() {
      this.showEmpty = false;
      // apiVolunteerWebDemandList 这个是我的请求接口(按照自己的接口写吧)
      apiVolunteerWebDemandList(this.dataForm).then(data => {
        console.log(data, '数acceptList据');
        this.loading = false;
        let length = 0;
        if (data.content) {
          length = data.content.length;
        } else {
          this.finished = true;
          this.showEmpty = true;
        }
        if (length) {
          this.acceptList = this.acceptList.concat(data.content);
          if (length < this.dataForm.pageSize) {
            this.finished = true;
          } else {
            this.dataForm.pageNow++;
          }
        } else {
          this.finished = true;
          if (this.dataForm.pageNow === 1) {
            this.showEmpty = true;
          }
        }
      });
    },

// 第二种方式
<script> 
    data() { 
      return {
     	refreshing: false,
        loading: true,
        loadingAll: true,
        finished: false,
        pageIndex: 1,
        pageSize: 10,
        list: [],
      }
	 }

      onLoad() {
        if (this.refreshing) {
          this.list = [];
          this.refreshing = false;
        }

        this.finished = false;
        this.loading = true;

        this.getList();
      },
      // 主页-查询banner资源位列表
      async getList() {
        const data = await apiYoungCardApiApiYouthInnInnPage({
           pageIndex: this.pageIndex,
          pageSize: this.pageSize,
        });
         this.list = this.list.concat(data.list);
        this.loading = false;
         this.loadingAll = false;
         // 数据全部加载完成
        if (this.list.length >= data.total) {
           this.finished = true;
        } else {
           this.pageIndex++;
        }
      },
</script>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值