基于Vant框架的下拉刷新和上滑加载

这里是基于了Vant-ui框架做的下拉刷新和上拉加载
没有用复杂原生JS来写,用框架非常的方便,一看就会,上手性也非常的强,同样效率也非常的高
废话不多说直接上代码,更多的API可以查看这里!!

下面的代码中我也改了一些参数,不过效果非常的好。
这里只是一些假数据模拟的,也可以用ajax来获取真是数据,效果更炸

<template>
  <van-pull-refresh v-model="isLoading" @refresh="onRefresh">
    <van-list
      v-model="loading"
      :finished="finished"
      finished-text="没有更多了"
      @load="onLoad"
      class="load"
    >
      <van-cell v-for="item in list" :key="item" :title="item" />
    </van-list>
  </van-pull-refresh>
</template>

<script>
import Vue from "vue";
import { List, Cell, PullRefresh,Toast } from "vant";
Vue.use(List);
Vue.use(Cell);
Vue.use(PullRefresh);
Vue.use(Toast);
export default {
  data() {
    return {
      list: [],
      loading: false,
      finished: false,
      isLoading: false,
    };
  },

  methods: {
    onLoad() {
      // 异步更新数据
      setTimeout(() => {
        for (let i = 0; i < 10; i++) {
          this.list.push(this.list.length + 1);
        }
        // 加载状态结束
        this.loading = false;
        // 数据全部加载完成
        if (this.list.length >= 50) {
          this.finished = true;
        }
      }, 500);
    },
    onRefresh() {
      setTimeout(() => {
        this.$toast('刷新成功');
        this.isLoading = false;
        this.count++;
      }, 500);
    }
  }
};
</script>

希望对大家有帮助!

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,针对您的问题,下面是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、付费专栏及课程。

余额充值