使用vant List列表,点击tab切换触发onLoad事件,造成两次请求

本文介绍了一种有效解决使用Vant List组件时,因点击Tab切换导致onLoad事件重复触发并引发多次请求的问题。通过与后端配合,在响应中包含当前页码信息,确保只有在指定条件下才更新数据列表,避免重复加载。

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

解决方法实际测试有效 让后端多返回一个当前请求的页码 与你要请求的页码是否相等

 if (res.code == 10000) {
            console.log(res);
            if (this.page === res.data.page) {
              if (this.page === 1) {
                this.patentList = list;
              } else {
                this.patentList = [...this.patentList, ...list];
              }
              this.loading = false;
              if (Math.ceil(num / 3) === this.page) {
                this.finished = true;
              }
              this.page += 1;
            }
          }

全部代码

import _ from "lodash";
export default {
  components: {},
  name: "Brochure",
  props: ["photo"],
  data() {
    return {
      patentList: [],
      photoType: [],
      limit: 3,
      page: 1,
      type: "",
      loading: false,
      finished: false,
    };
  },
  created() {
    this.photoType = this.photo.cate;
  },
  watch: {
    type() {
      if (this.page === 1) {
        this.finished = false;
        this.patentList = [];
        this.getList();
      }
    },
  },
  methods: {
    getTitle(index, title) {
      // console.log(index, title);
      if (index === 0) {
        // console.log("当前为零");
        this.type = "";
      } else {
        this.type = title;
      }
      this.page = 1;
    },
    getList() {
      this.loading = true;
      console.log("被调用了", this.page);
      this.$util.api(
        "/app/bazaar/picture",
        {
          store_id: this.$route.query.id * 1,
          limit: this.limit,
          page: this.page,
          type: this.type,
        },
        (res) => {
          const { num, list } = res.data;
          if (res.code == 10000) {
            console.log(res);
            if (this.page === res.data.page) {
              if (this.page === 1) {
                this.patentList = list;
              } else {
                this.patentList = [...this.patentList, ...list];
              }
              this.loading = false;
              if (Math.ceil(num / 3) === this.page) {
                this.finished = true;
              }
              this.page += 1;
            }
          }
        }
      );
    },
    onLoad: _.throttle(function () {
      this.getList();
    }, 1000),
  },
};
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值