vue滑动分页加载数据

vue滑动分页加载数据


  data() {
      return {
        date: "",
        week: "",
        list: null,
        loading: false,
        total: 0,
        hasMore: true,
        listQuery: {
          page: 1,
          size: 10
        },
      };
    },
  beforeDestroy() {
      console.log("触发:beforeDestroy")
      window.removeEventListener("scroll", this.handleScroll());
    },
    created() {

       console.log("触发:created")
      this.fetchData();
      window.addEventListener("scroll", this.handleScroll,true);
    },
    methods: {
      async fetchNewData() {
        this.loading = true;
        try {
          var vm = this;
          this.axios({
            method: 'post',
            url: 'http://localhost:7003/llcBk/web/page',
            data: {
              "page": ++vm.listQuery.page,
              "size": vm.listQuery.size
            }
          }).then(function(response) {
            console.log("触发fetchNewData()方法:"+response.data)
            if (response.data.data.list.length === 0) {
              vm.hasMore = false;
              vm.listQuery.page--;
            } else {
              vm.list =vm.list.concat(response.data.data.list);
              vm.listQuery.page=response.data.data.page,
              vm.listQuery.size=response.data.data.size,
              vm.total = response.data.data.total
            }
            vm.loading = false;
          })
        } catch (e) {
          this.$message.error(e.Msg);
        }
      },
      handleScroll() {
        console.log("触发handleScroll()方法")
        const scrollTop = document.documentElement.scrollTop;
        const scrollHeight = document.documentElement.scrollHeight;
        const clientHeight = document.documentElement.clientHeight;
        const temp = clientHeight + Math.floor(scrollTop);
        if (this.hasMore) {
          if (
            temp === scrollHeight ||
            temp === scrollHeight + 1 ||
            temp === scrollHeight - 1
          ) {
            if (!this.loading) {
              this.fetchNewData();
            }
          }
        }
      },
      // 获取博客数据
      async fetchData() {
        var vm = this;
        this.axios({
          method: 'post',
          url: 'http://localhost:7003/llcBk/web/page',
          data: {
            "page": vm.listQuery.page,
            "size": vm.listQuery.size
          }
        }).then(function(response) {
            console.log("触发fetchData()方法:"+response.data.data.list),
            vm.list = response.data.data.list,
            vm.listQuery.page=response.data.data.page,
            vm.listQuery.size=response.data.data.size,
            vm.total = response.data.data.total
        })
      }
    },
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
要实现滑动分页列表,可以使用Vue.js配合一些插件来完成,以下是一些步骤: 1. 安装Vue.js并创建Vue实例; 2. 引入Vue插件vue-infinite-loading,它提供了无限滚动加载数据的功能; 3. 创建一个包含列表数据的组件,使用v-for指令来渲染列表; 4. 在组件中使用vue-infinite-loading的指令,在滚动到底部时自动加载下一页数据; 5. 监听滚动事件,当滚动到底部时触发加载数据; 6. 在组件中使用CSS样式来实现滑动效果。 以下是一个简单的示例代码: ```html <template> <div class="list-container" ref="listContainer"> <div v-for="(item, index) in listData" :key="index" class="list-item">{{ item }}</div> <div v-infinite-loading="loadMore" :infinite-disabled="isLoading" infinite-distance="10%"> <div v-if="isLoading"><i class="fa fa-spinner fa-spin"></i> 加载中...</div> <div v-else><i class="fa fa-arrow-down"></i> 下拉加载更多</div> </div> </div> </template> <script> import InfiniteLoading from 'vue-infinite-loading'; export default { components: { InfiniteLoading, }, data() { return { listData: [], currentPage: 1, isLoading: false, }; }, mounted() { this.loadData(); window.addEventListener('scroll', this.handleScroll); }, methods: { loadData() { // 模拟异步加载数据 this.isLoading = true; setTimeout(() => { for (let i = 0; i < 10; i++) { this.listData.push(`Item ${this.listData.length + 1}`); } this.currentPage++; this.isLoading = false; }, 1000); }, loadMore() { this.loadData(); }, handleScroll() { const container = this.$refs.listContainer; const scrollTop = container.scrollTop; const scrollHeight = container.scrollHeight; const clientHeight = container.clientHeight; if (scrollTop + clientHeight >= scrollHeight) { this.loadData(); } }, }, }; </script> <style> .list-container { height: 300px; overflow-y: scroll; } .list-item { height: 50px; line-height: 50px; border-bottom: 1px solid #eee; } </style> ``` 在上面的示例中,我们使用了vue-infinite-loading插件来实现滑动分页列表的功能。同时,我们还监听了滚动事件,在滚动到底部时触发自动加载下一页数据。通过CSS样式设置了列表的高度和滑动效果。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

爱上编程2705

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值