vant-list @load加载问题

vant-list @load加载 不断请求接口问题个人解决方案

  1. 在vant-list 标签中配置offset属性,默认值300太大了(建议在30左右)
  2. 有配置van-pull-refresh使用的, 在vant-list 中 设置 :immediate-check="false"
  3. 设定一个变量,例如:isLoad,在每次请求方法时拦截多余的请求
async pageMore() {
                if (!this.isLoad) {
                    this.isLoad = true;
                    this.loadStart();
                    await this.$postData("xxx", this.queryParam, {}).then((res) => {
                        if (res.status === 200) {
                            res.data.records.forEach(v => {
                                this.dataList.push(v);
                            });
                            const list = res.data.records;
                            if (list === null || list.length === 0 ||
                                this.queryParam.size * this.queryParam.current >= res.data.total
                                || this.dataList.length >= res.data.total) {
                                this.finished = true;
                            }
                            this.queryParam.current++;
                        } else {
                            this.$toast.fail(res.msg);
                        }
                    }).catch(e => {
                        this.$toast.fail(e.message);
                    }).finally(() => {
                        // this.refreshing = false;
                        this.loadEnd();
                        this.isLoad = false;
                    });
                }
            }

目前没找到和想到更好的方案,有哪位朋友有更好的方案,请评论区说一下哈。

好的,针对您的问题,下面是vant-list实现下拉刷新和上拉加载的方法: 1. 安装vant 首先需要在项目中安装vant,可以通过npm或yarn进行安装: npm install vant --save 或者 yarn add vant 2. 引入vant 在需要使用vant的页面中引入vant: import Vue from 'vue'; import Vant from 'vant'; import 'vant/lib/index.css'; Vue.use(Vant); 3. 使用vant-list 在页面中使用vant-list组件,用于展示列表数据: <van-list :finished="finished" @load="onLoad" @refresh="onRefresh"> <!-- 列表项 --> </van-list> 其中,@load和@refresh分别是上拉加载和下拉刷新的事件,finished用于控制是否已加载完所有数据。 4. 实现下拉刷新 在页面的methods中定义onRefresh方法,用于处理下拉刷新事件: export default { data() { return { list: [], // 列表数据 page: 1, // 当前页码 pageSize: 10, // 每页数据数量 finished: false // 是否已加载完所有数据 }; }, methods: { async onRefresh() { this.page = 1; // 下拉刷新时重置页码 this.list = []; // 清空列表数据 await this.loadData(); // 加载数据 this.finished = false; // 重置finished状态 }, async loadData() { // 根据当前页码和每页数据数量加载数据 const data = await api.getList(this.page, this.pageSize); if (data.length > 0) { this.list = this.list.concat(data); // 将数据添加到列表中 this.page++; // 页码+1 } else { this.finished = true; // 已加载完所有数据 } } } }; 5. 实现上拉加载 在页面的methods中定义onLoad方法,用于处理上拉加载事件: export default { data() { return { list: [], // 列表数据 page: 1, // 当前页码 pageSize: 10, // 每页数据数量 finished: false // 是否已加载完所有数据 }; }, methods: { async onRefresh() { this.page = 1; // 下拉刷新时重置页码 this.list = []; // 清空列表数据 await this.loadData(); // 加载数据 this.finished = false; // 重置finished状态 }, async onLoad() { if (this.finished) return; // 如果已加载完所有数据则直接返回 await this.loadData(); // 加载数据 }, async loadData() { // 根据当前页码和每页数据数量加载数据 const data = await api.getList(this.page, this.pageSize); if (data.length > 0) { this.list = this.list.concat(data); // 将数据添加到列表中 this.page++; // 页码+1 } else { this.finished = true; // 已加载完所有数据 } } } }; 通过以上步骤,就可以实现vant-list的下拉刷新和上拉加载功能了。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值