欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

vant上拉刷新,下拉加载

程序员文章站 2024-03-23 23:07:28
...
<template>
  <div id="video_wrap">
     <div class="video_tab">
      <van-pull-refresh v-model="isLoading" @refresh="onRefresh">
        <van-list
          v-model="loading"
          :finished="finished"
          @load="onLoad"
        >
         </van-list>
        <div class="no_more" v-show="finished">---到底了哦---.</div>
      </van-pull-refresh>
    </div>
  </div>
</template>

<script>
    export default {
      data(){
        return{
          videoList: [],//用于存放加载的数据
          loading: false,//控制上拉加载的加载动画
          finished: false,//控制在页面往下移动到底部时是否调用接口获取数据
          isLoading: false,//控制下拉刷新的加载动画
          pageNum: 1, // 页数
          offset: 0,  // 下次加载起始页
          totalPage: 0 // 总页数
        }
      },

      methods:{
        async init(){
          let that = this;
          let params = {
            offset: (that.pageNum - 1) * 8
          };
         let res = await this.$http.get(this.$HOST + '/open/health/message/center/app/videoList', params);
          that.videoList = res.result.videosAll.data; // 第一页内容
          that.pageNum = res.result.videosAll.page; // 当前显示页
          that.totalPage = Math.ceil(res.result.videosAll.total / res.result.videosAll.limit); // 总页数
           if(res.result.videosAll.total<10) {
             that.finished = true; // 加载结束
             that.isLoading = false;
             that.loading = false;
             return false;
          }
        },

        async concatData(){
          let that = this;
          that.pageNum += 1;
          // 数据全部加载完成
          let params = {
            offset: (that.pageNum - 1) * 8
          };
         let res = await this.$http.get(this.$HOST + '/open/health/message/center/app/videoList', params);
         if(res.result.videosAll.total<10) {
            that.finished = true; // 加载结束
            that.isLoading = false;
            that.loading = false;
            return false;
          }
          that.videoList = that.videoList.concat(res.result.videosAll.data);
          this.loading = false;
          if(this.pageNum >= this.totalPage){
            that.finished = true; // 加载结束
            that.isLoading = false;
            that.loading = false;
          }
       },
      onRefresh() {
          setTimeout(() => {
            this.$toast({
              message: '刷新成功',
              position: 'bottom'
            });
            this.isLoading = false;
            this.pageNum = 1;
            this.loading = false;
            this.finished = false;
            this.isLoading = false;
            this.init();
          }, 500);
        },

        onLoad() {
          // 异步更新数据
          setTimeout(() => {
            this.concatData();
          }, 500);
        }
      },
      async created(){
        this.init();
      }
    }
</script>

亲测可用~~~~~,自己整理下的

上一篇: Hello 帧动画

下一篇: