当前位置:首页 > VUE

vue实现分页累加

2026-01-19 08:13:29VUE

Vue实现分页累加

基础思路

分页累加通常指滚动加载更多数据时,保留已加载内容并追加新数据。Vue中可通过监听滚动事件或结合第三方库实现。

核心代码实现

数据定义

data() {
  return {
    items: [],       // 已加载数据
    page: 1,         // 当前页码
    isLoading: false, // 加载状态
    hasMore: true    // 是否还有更多数据
  }
}

加载方法

methods: {
  loadMore() {
    if (this.isLoading || !this.hasMore) return

    this.isLoading = true
    fetch(`/api/data?page=${this.page}`)
      .then(res => res.json())
      .then(newItems => {
        this.items = [...this.items, ...newItems]
        this.page++
        this.hasMore = newItems.length > 0
      })
      .finally(() => this.isLoading = false)
  }
}

滚动监听实现

模板部分

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

滚动处理方法

handleScroll(e) {
  const { scrollTop, clientHeight, scrollHeight } = e.target
  const bottomReached = scrollTop + clientHeight >= scrollHeight - 100

  if (bottomReached) this.loadMore()
}

使用IntersectionObserver优化

观察器实现

mounted() {
  const observer = new IntersectionObserver((entries) => {
    if (entries[0].isIntersecting) this.loadMore()
  })

  this.$nextTick(() => {
    observer.observe(this.$refs.loadingTrigger)
  })
}

模板对应修改

vue实现分页累加

<div v-for="item in items">{{ item.text }}</div>
<div ref="loadingTrigger" v-if="!isLoading && hasMore"></div>
<div v-if="isLoading">加载中...</div>

注意事项

  • 节流处理:滚动事件建议添加节流函数
  • 错误处理:网络请求需添加catch处理
  • 组件销毁时移除观察者
  • 移动端需考虑touch事件兼容性

完整示例组件

export default {
  data() {
    return {
      items: [],
      page: 1,
      isLoading: false,
      hasMore: true
    }
  },
  methods: {
    async loadMore() {
      if (this.isLoading || !this.hasMore) return

      this.isLoading = true
      try {
        const res = await fetch(`/api/data?page=${this.page}`)
        const newItems = await res.json()
        this.items.push(...newItems)
        this.hasMore = newItems.length > 0
        this.page++
      } finally {
        this.isLoading = false
      }
    }
  },
  mounted() {
    window.addEventListener('scroll', this.handleScroll)
  },
  beforeDestroy() {
    window.removeEventListener('scroll', this.handleScroll)
  }
}

标签: 分页vue
分享给朋友:

相关文章

vue的艾特功能实现

vue的艾特功能实现

Vue 的 @ 功能实现 在 Vue 中实现类似社交平台的 @ 功能,通常涉及输入框的监听、用户匹配和选择插入。以下是具体实现方法: 监听输入框内容 使用 v-model 绑定输入框内容,并通过 @…

vue实现绑卡的原理

vue实现绑卡的原理

Vue 实现绑卡的原理 Vue 实现绑卡功能通常涉及前端表单处理、数据绑定、验证以及和后端 API 的交互。以下是实现绑卡功能的核心原理和步骤: 数据绑定与表单处理 Vue 通过 v-model 指…

vue 实现列表

vue 实现列表

Vue 实现列表的方法 在 Vue 中实现列表通常使用 v-for 指令,结合数组或对象数据渲染动态列表。以下是几种常见的实现方式: 基础列表渲染 使用 v-for 指令遍历数组,渲染列表项。ite…

vue实现签章

vue实现签章

Vue 实现签章功能 签章功能通常包括手写签名、电子印章等。以下是基于 Vue 的实现方法: 使用 canvas 实现手写签名 安装依赖(如需要): npm install signature_p…

proxy实现vue

proxy实现vue

使用 Proxy 实现 Vue 响应式系统 Vue 3 的响应式系统基于 JavaScript 的 Proxy API,通过代理对象实现对属性的拦截和依赖追踪。以下是实现的核心逻辑: 创建响应式对象…

vue实现模糊

vue实现模糊

Vue实现模糊搜索的方法 在Vue中实现模糊搜索功能通常需要结合输入框和列表渲染,通过监听输入内容动态过滤数据。以下是几种常见实现方式: 使用计算属性实现 计算属性适合处理需要响应式更新的搜索逻辑:…