记录一个van-list上拉加载更多在非第一页切换列表请求数据页数不正确问题

<van-list
  v-model="loading"
  :finished="finished"
  finished-text="没有更多了"
  @load="load"
  offset="50"
  :immediate-check="false"
>
列表
</van-list>
	// 初始化分页列表
    initList() {
      this.currentPage = 1;
      this.list = [];
      this.loading = false;
      this.finished = false;
      this.orderList();
    },
	// 获取订单列表
    async orderList() {
      let params = {
        token: window.sessionStorage.getItem("token") || "",
        page: this.currentPage,
        size: 10,
        order_type: this.$parent.order,
        search_words: this.search || "",
      };
      console.log("参数", params);
      params = this.$public170.webEncrypt(JSON.stringify(params));
      let { data } = await this.$axios.post(this.$api.orderList, {
        params: params,
      });
      if (data.code != 1000) return this.$public170.dataAlert(data.msg);
      let ost = JSON.parse(this.$public170.webDecrypt(data.data));
      console.log("获取订单列表返回", ost);

      // 解除防抖
      this.$emit("update:disbounce", false);

      this.list.push(...ost.order_list);
      this.total = this.list.length;
      this.loading = false;

      if (ost.order_list.length <= 0) {
        this.finished = true;
      } else {
        this.finished = false;
      }
    },
    // 全部列表加载更多
    load() {
      console.log("加载更多");
      if (this.list.length !== this.total) return;
      this.currentPage = this.currentPage + 1;
      this.orderList();
    },

问题是这样,这个分页接口后端未返回总页数,需要前端进行处理最后一页的问题;目前是根据返回的数组长度小于等于0来作为临界点。当在第一个列表请求到非第一页(currentPage>1时)的数据,切换到另外的一个列表,虽然会在切换之前进行分页数据初始化,但是在切换后会直接去触发加载下一页事件(load事件),由此,当我再切换回去的时候,直接调用了两次列表数据(第一页和第二页),接口第二页的返回时间比第一页的返回时间要短,在页面渲染数据时,第二页的数据在列表的前面,第二页的数据在列表的后面展示。
解决方案:在数据返回时记录返回的数据长度,在进行触发加载更多时进行限制判断是否请求下一页数据。
未知领域:为什么在页数大于一的时候切换列表会去触发加载更多呢?

更新:找到为什么会出现连续加载多次的原因了,
需要在orderList方法中发起请求前将loading设置为true状态,在请求返回后再关闭为false。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值