当前位置:首页 > VUE

vue实现页面加载更多

2026-02-21 12:20:23VUE

Vue 实现页面加载更多功能

使用滚动事件监听

通过监听滚动事件判断是否到达页面底部,触发加载更多数据。在 mounted 钩子中添加滚动事件监听,在 beforeDestroy 钩子中移除监听以避免内存泄漏。

mounted() {
  window.addEventListener('scroll', this.handleScroll);
},
beforeDestroy() {
  window.removeEventListener('scroll', this.handleScroll);
},
methods: {
  handleScroll() {
    const scrollTop = document.documentElement.scrollTop || document.body.scrollTop;
    const windowHeight = window.innerHeight;
    const scrollHeight = document.documentElement.scrollHeight || document.body.scrollHeight;
    if (scrollTop + windowHeight >= scrollHeight - 100) {
      this.loadMore();
    }
  },
  loadMore() {
    if (this.isLoading || this.noMoreData) return;
    this.isLoading = true;
    // 调用API获取更多数据
    fetchMoreData().then(res => {
      this.list = [...this.list, ...res.data];
      this.isLoading = false;
      if (res.data.length === 0) {
        this.noMoreData = true;
      }
    });
  }
}

使用 IntersectionObserver API

利用现代浏览器支持的 IntersectionObserver 实现更高效的底部检测,避免频繁触发滚动事件。

vue实现页面加载更多

mounted() {
  const observer = new IntersectionObserver((entries) => {
    if (entries[0].isIntersecting) {
      this.loadMore();
    }
  });
  observer.observe(this.$refs.loadMoreTrigger);
},
methods: {
  loadMore() {
    if (this.isLoading || this.noMoreData) return;
    this.isLoading = true;
    fetchMoreData().then(res => {
      this.list = [...this.list, ...res.data];
      this.isLoading = false;
      if (res.data.length === 0) {
        this.noMoreData = true;
      }
    });
  }
}

结合分页参数

在请求数据时携带分页参数,确保每次加载更多数据时获取正确的下一页数据。

data() {
  return {
    list: [],
    page: 1,
    pageSize: 10,
    isLoading: false,
    noMoreData: false
  };
},
methods: {
  loadMore() {
    if (this.isLoading || this.noMoreData) return;
    this.isLoading = true;
    fetchData(this.page, this.pageSize).then(res => {
      this.list = [...this.list, ...res.data];
      this.page++;
      this.isLoading = false;
      if (res.data.length < this.pageSize) {
        this.noMoreData = true;
      }
    });
  }
}

使用第三方库

对于复杂场景,可以使用第三方库如 vue-infinite-loading 简化实现。

vue实现页面加载更多

安装依赖:

npm install vue-infinite-loading

使用示例:

<template>
  <div>
    <div v-for="item in list" :key="item.id">{{ item.name }}</div>
    <infinite-loading @infinite="loadMore"></infinite-loading>
  </div>
</template>

<script>
import InfiniteLoading from 'vue-infinite-loading';
export default {
  components: { InfiniteLoading },
  methods: {
    loadMore($state) {
      fetchMoreData().then(res => {
        this.list = [...this.list, ...res.data];
        if (res.data.length) {
          $state.loaded();
        } else {
          $state.complete();
        }
      });
    }
  }
};
</script>

注意事项

  • 避免重复请求:在加载数据时设置 isLoading 标志位,防止重复触发。
  • 数据为空处理:当返回数据为空时,标记 noMoreData 并显示提示信息。
  • 性能优化:对于大量数据,考虑虚拟滚动技术(如 vue-virtual-scroller)提升渲染性能。
  • 移动端适配:在移动端可能需要调整触发加载的阈值,确保用户体验流畅。

标签: 加载页面
分享给朋友:

相关文章

vue实现页面截屏

vue实现页面截屏

使用html2canvas库实现截屏 安装html2canvas库 npm install html2canvas --save 在Vue组件中引入并使用 import html2canvas f…

vue实现加载

vue实现加载

Vue 实现加载功能的方法 使用 v-if 或 v-show 控制加载状态 在模板中通过 v-if 或 v-show 控制加载动画的显示与隐藏。v-if 会完全移除 DOM 元素,而 v-show 仅…

jquery页面

jquery页面

jQuery 页面操作指南 jQuery 是一个快速、简洁的 JavaScript 库,简化了 HTML 文档遍历、事件处理、动画和 Ajax 交互的操作。以下是 jQuery 在页面中的常见使用方法…

h5实现页面切换

h5实现页面切换

h5实现页面切换的方法 在H5中实现页面切换可以通过多种方式完成,包括使用原生HTML链接、JavaScript动态加载、框架路由等。以下是几种常见的方法: 使用原生HTML的<a>标…

vue 实现页面跳转

vue 实现页面跳转

Vue 实现页面跳转的方法 在 Vue 中实现页面跳转可以通过多种方式,主要包括使用 Vue Router 提供的导航方法和原生 JavaScript 的方式。 使用 Vue Router 的 r…

vue实现关闭页面

vue实现关闭页面

关闭当前页面的方法 在Vue中关闭当前页面通常涉及调用浏览器提供的JavaScript方法。以下是几种常见的实现方式: window.close()方法 methods: { closeWin…