vant ui 上拉加载下 拉刷新踩坑记

本文介绍如何使用Vant UI框架的列表组件实现下拉刷新和上拉加载更多功能,并通过调整参数避免初始化时的重复请求,提升用户体验。

直接上代码
template

<template>
	<van-pull-refresh v-model="refreshing" @refresh="onRefresh">
	   <van-list
	     v-model="loading"
	     :finished="finished"
	     :offset="1"
	     finished-text="没有更多了"
	     @load="onLoad"
	     :immediate-check="false"
	     class="cansee"
	   >
	     <card
	       v-for="item in list"
	       :id="item.id"
	       :key="item.id"
	       :title="item.title"
	       :time="item.pushTime"
	       :content="item.subTitle"
	     />
	   </van-list>
	 </van-pull-refresh>
 </template>
 <script>
data() {
    return {
      list: [],
      loading: false,
      finished: false,
      refreshing: false,
      pageIndex: 1,
      carpageTotal: 1
    };
  },
  methods:{
  getPageList(pageIndex) {
      const that = this;
      that.list = [];
      const pageSize = 10;
      $.ajax({
        type: "get",
        url:
          that.$config.baseURL +
          `/schNotice/appNotice/schList/${pageIndex}/${pageSize}`,
        success: function(res) {
          if (res.code == 0) {
            that.loading = false;
            if (res.result.list.length > 0) {
              that.refreshing = false;
              that.list = res.result.list;
            } else {
              that.refreshing = false;
              that.list = [];
            }
          } else {
            that.loading = false;
          }
        }
      });
    },
	onRefresh() {
      this.pageIndex = 1;
      this.finished = false;
      this.refreshing = true;
      this.getPageList(1);
    },
	onLoad() {
      const that = this;
      that.loading = true;
      that.pageIndex = that.pageIndex + 1;
      $.ajax({
        type: "get",
        url:
          that.$config.baseURL +
          `/schNotice/appNotice/schList/${that.pageIndex}/10`,
        success: function(res) {
          if (res.code == 0) {
            that.loading = false;
            if (res.result.list.length < 10) {
              that.finished = true;
            }
            if (res.result.list.length === 0) {
              that.loading = false;
              that.finished = true;
            }
            //判断当前返回数据的pageIndex 是否和当前的pageIndex相等,相等就把之前的数据和当前返回的数据合并(避免数据重复)
            if (res.result.pagination.pageIndex == that.pageIndex) {
              that.list = that.list.concat(res.result.list);
            }
          } else {
            that.loading = false;
            that.finished = true;
          }
        }
      });
    },

  },
  mounted(){
	this.getPageList(1)
  }
  
</script>

用过vant的小伙伴应该知道,初始化的时候会发现掉用啦两次接口,当然官网说的很清楚,需要在vant-list上设置:immediate-check=“false”,然后在mounted的时候自己手动调用接口,这样就避免啦连续调用的问题啦

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值