当前位置:首页 > VUE

vue实现上拉加载

2026-01-23 01:26:12VUE

Vue 实现上拉加载

使用 Intersection Observer API

Intersection Observer API 可以监听元素是否进入视口,适合实现上拉加载。

<template>
  <div>
    <div v-for="item in list" :key="item.id">{{ item.content }}</div>
    <div ref="loader" v-if="!isLoading && hasMore">加载更多...</div>
    <div v-if="isLoading">加载中...</div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      list: [],
      isLoading: false,
      hasMore: true,
      page: 1
    };
  },
  mounted() {
    this.initObserver();
    this.loadData();
  },
  methods: {
    initObserver() {
      const observer = new IntersectionObserver((entries) => {
        if (entries[0].isIntersecting && !this.isLoading && this.hasMore) {
          this.loadData();
        }
      });
      observer.observe(this.$refs.loader);
    },
    async loadData() {
      this.isLoading = true;
      try {
        const newData = await fetchData(this.page);
        this.list = [...this.list, ...newData];
        this.page++;
        this.hasMore = newData.length > 0;
      } catch (error) {
        console.error(error);
      } finally {
        this.isLoading = false;
      }
    }
  }
};
</script>

使用滚动事件监听

通过监听滚动事件,计算滚动位置触发加载。

<template>
  <div ref="container" @scroll="handleScroll">
    <div v-for="item in list" :key="item.id">{{ item.content }}</div>
    <div v-if="isLoading">加载中...</div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      list: [],
      isLoading: false,
      hasMore: true,
      page: 1
    };
  },
  mounted() {
    this.loadData();
  },
  methods: {
    handleScroll() {
      const container = this.$refs.container;
      const scrollBottom = container.scrollHeight - container.scrollTop - container.clientHeight;
      if (scrollBottom < 50 && !this.isLoading && this.hasMore) {
        this.loadData();
      }
    },
    async loadData() {
      this.isLoading = true;
      try {
        const newData = await fetchData(this.page);
        this.list = [...this.list, ...newData];
        this.page++;
        this.hasMore = newData.length > 0;
      } catch (error) {
        console.error(error);
      } finally {
        this.isLoading = false;
      }
    }
  }
};
</script>

使用第三方库

Vue-infinite-loading 是一个专门用于无限滚动的 Vue 插件。

安装:

npm install vue-infinite-loading

使用:

vue实现上拉加载

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

<script>
import InfiniteLoading from 'vue-infinite-loading';

export default {
  components: {
    InfiniteLoading
  },
  data() {
    return {
      list: [],
      page: 1
    };
  },
  methods: {
    async loadData($state) {
      try {
        const newData = await fetchData(this.page);
        this.list = [...this.list, ...newData];
        this.page++;
        if (newData.length) {
          $state.loaded();
        } else {
          $state.complete();
        }
      } catch (error) {
        $state.error();
      }
    }
  }
};
</script>

注意事项

  • 确保在组件销毁时移除事件监听或断开 Intersection Observer。
  • 添加防抖或节流避免频繁触发加载。
  • 处理加载失败的情况,提供重试机制。
  • 移动端需要考虑滚动性能优化。

标签: 加载vue
分享给朋友:

相关文章

vue树形实现

vue树形实现

Vue 树形组件实现方法 使用递归组件实现树形结构 递归组件是 Vue 中实现树形结构的常见方法。通过组件调用自身的方式,可以轻松构建多层嵌套的树形结构。 <template> &l…

vue实现长按

vue实现长按

Vue 实现长按功能的方法 在 Vue 中实现长按功能可以通过原生事件监听或自定义指令完成。以下是几种常见实现方式: 使用原生事件监听 通过 @mousedown 和 @mouseup 或 @tou…

vue 实现excel

vue 实现excel

Vue 实现 Excel 功能 前端导出 Excel 使用 xlsx 库可以轻松实现前端 Excel 导出功能。安装依赖: npm install xlsx 导出 Excel 示例代码: impo…

vue广播实现

vue广播实现

vue广播实现 在Vue中实现广播功能可以通过事件总线(Event Bus)或Vuex来完成。以下是两种常见的实现方式: 使用事件总线(Event Bus) 事件总线是一种简单的跨组件通信方式,适用…

vue实现打印

vue实现打印

使用Vue实现打印功能 在Vue项目中实现打印功能可以通过多种方式完成,以下是几种常见的方法: 方法一:使用window.print() 这种方法适用于打印整个页面或特定区域的内容。 // 在Vu…

vue实现组件

vue实现组件

Vue 组件实现基础 Vue 组件是可复用的 Vue 实例,用于封装 UI 和逻辑。通过 .vue 文件或直接注册组件实现。 单文件组件 (SFC) 示例 <template>…