当前位置:首页 > VUE

vue实现分页累加

2026-01-19 08:13:29VUE

Vue实现分页累加

基础思路

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

核心代码实现

数据定义

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

加载方法

vue实现分页累加

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>

滚动处理方法

vue实现分页累加

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)
  })
}

模板对应修改

<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 Router 的路由配置可以天然转化为菜单结构,适合权限与路由绑定的系统: // router/index.js const route…

javascript实现vue

javascript实现vue

Vue.js 的基本实现 在 JavaScript 中实现 Vue.js 的核心功能,主要包括数据绑定、响应式系统和虚拟 DOM。以下是一个简化的实现示例。 数据响应式系统 通过 Object.de…

vue实现布局

vue实现布局

Vue 实现布局方法 Vue 提供了多种方式实现页面布局,可以通过组件化、CSS 框架或自定义样式完成。以下是几种常见方法: 使用 CSS Flexbox 或 Grid 通过 Vue 单文件组件结合…

vue监听实现

vue监听实现

Vue 监听实现方法 在 Vue 中可以通过多种方式实现监听数据变化或事件触发,以下是常见的几种方法: 使用 watch 选项 watch 是 Vue 提供的核心功能之一,用于观察和响应 Vue 实…

vue实现刷卡

vue实现刷卡

Vue 实现刷卡效果 在 Vue 中实现刷卡效果可以通过 CSS 动画和 Vue 的过渡系统结合完成。以下是一种常见的实现方式: 基础实现方法 安装 Vue 过渡依赖(如果尚未安装): npm i…

vue实现github

vue实现github

Vue 实现 GitHub 相关功能 创建 GitHub API 请求 在 Vue 项目中,可以使用 axios 或 fetch 与 GitHub API 进行交互。需要在 GitHub 上创建个人访…