当前位置:首页 > VUE

vue 实现下拉加载

2026-02-22 18:38:14VUE

Vue 实现下拉加载的方法

使用 IntersectionObserver API

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

<template>
  <div>
    <div v-for="item in list" :key="item.id">{{ item.content }}</div>
    <div ref="loadMore"></div>
  </div>
</template>

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

使用 window.scroll 事件

通过监听滚动事件判断是否到达底部。

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

<script>
export default {
  data() {
    return {
      list: [],
      page: 1,
      isLoading: false
    }
  },
  mounted() {
    this.loadData()
    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.isLoading) {
        this.loadData()
      }
    },
    async loadData() {
      this.isLoading = true
      const newData = await fetchData(this.page)
      this.list = [...this.list, ...newData]
      this.page++
      this.isLoading = false
    }
  }
}
</script>

使用第三方库

可以使用 vue-infinite-loading 等第三方库简化实现。

安装:

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)
        if (newData.length) {
          this.list = [...this.list, ...newData]
          this.page++
          $state.loaded()
        } else {
          $state.complete()
        }
      } catch (error) {
        $state.error()
      }
    }
  }
}
</script>

注意事项

  • 添加加载状态提示避免重复请求
  • 在组件销毁时移除事件监听
  • 考虑数据加载完成后的处理
  • 添加错误处理机制
  • 对于移动端需要考虑滚动性能优化

以上方法可以根据具体需求选择适合的实现方式,IntersectionObserver API 是现代浏览器推荐的方式,而第三方库可以快速实现复杂功能。

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

相关文章

vue 页签实现

vue 页签实现

Vue 页签实现方法 在 Vue 中实现页签功能通常涉及动态组件、路由或状态管理。以下是几种常见的实现方式: 使用动态组件 通过 Vue 的 <component> 动态组件结合 v-f…

vue实现人脸识别比对

vue实现人脸识别比对

Vue 实现人脸识别比对的方法 使用现成的人脸识别 API 通过调用第三方人脸识别 API(如 Face++、百度 AI、腾讯云等)实现比对功能。注册开发者账号获取 API Key 和 Secret,…

vue实现过程

vue实现过程

Vue 的实现过程 Vue 的实现过程可以分为多个核心模块,包括响应式系统、虚拟 DOM、模板编译等。以下是 Vue 的主要实现过程分析。 响应式系统 Vue 的响应式系统基于 Object.def…

简单实现vue

简单实现vue

Vue 的基本实现 创建一个简单的 Vue 应用需要引入 Vue 库,并编写基本的模板和数据绑定逻辑。 <!DOCTYPE html> <html lang="en"> &l…

vue实现grid

vue实现grid

Vue 实现 Grid 布局的方法 使用 CSS Grid 布局 Vue 可以结合 CSS Grid 布局实现灵活的网格系统。CSS Grid 是现代浏览器原生支持的布局方案,无需额外依赖库。 &l…

vue 实现排序

vue 实现排序

Vue 实现排序的方法 使用计算属性实现排序 通过计算属性对数组进行排序,可以保持原始数据不变。示例代码展示了如何对列表按名称升序排序: <template> <div>…