当前位置:首页 > VUE

vue实现分页加载

2026-02-18 14:41:58VUE

实现分页加载的基本思路

Vue中实现分页加载通常结合前端分页和后端分页两种方式。前端分页适用于数据量较小的情况,后端分页适用于大数据量场景,通过API接口按需请求数据。

前端分页实现方法

安装依赖(如需要):

npm install vue-paginate

模板部分示例:

<template>
  <div>
    <ul>
      <li v-for="item in paginatedItems" :key="item.id">{{ item.name }}</li>
    </ul>
    <button @click="prevPage" :disabled="currentPage === 1">上一页</button>
    <span>{{ currentPage }} / {{ totalPages }}</span>
    <button @click="nextPage" :disabled="currentPage === totalPages">下一页</button>
  </div>
</template>

脚本部分:

export default {
  data() {
    return {
      allItems: [], // 所有数据
      currentPage: 1,
      itemsPerPage: 10
    }
  },
  computed: {
    totalPages() {
      return Math.ceil(this.allItems.length / this.itemsPerPage)
    },
    paginatedItems() {
      const start = (this.currentPage - 1) * this.itemsPerPage
      const end = start + this.itemsPerPage
      return this.allItems.slice(start, end)
    }
  },
  methods: {
    nextPage() {
      if (this.currentPage < this.totalPages) {
        this.currentPage++
      }
    },
    prevPage() {
      if (this.currentPage > 1) {
        this.currentPage--
      }
    }
  }
}

后端分页实现方法

模板部分:

<template>
  <div>
    <ul>
      <li v-for="item in items" :key="item.id">{{ item.name }}</li>
    </ul>
    <button @click="loadPrev" :disabled="!hasPrev">上一页</button>
    <button @click="loadNext" :disabled="!hasNext">下一页</button>
  </div>
</template>

脚本部分:

export default {
  data() {
    return {
      items: [],
      currentPage: 1,
      hasNext: false,
      hasPrev: false
    }
  },
  created() {
    this.fetchData()
  },
  methods: {
    async fetchData() {
      const response = await axios.get(`/api/items?page=${this.currentPage}`)
      this.items = response.data.items
      this.hasNext = response.data.hasNext
      this.hasPrev = response.data.hasPrev
    },
    loadNext() {
      if (this.hasNext) {
        this.currentPage++
        this.fetchData()
      }
    },
    loadPrev() {
      if (this.hasPrev) {
        this.currentPage--
        this.fetchData()
      }
    }
  }
}

无限滚动分页实现

结合v-infinite-scroll指令实现无限滚动:

<template>
  <div v-infinite-scroll="loadMore" infinite-scroll-disabled="busy" infinite-scroll-distance="10">
    <div v-for="item in items" :key="item.id">{{ item.name }}</div>
  </div>
</template>

脚本部分:

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

      this.busy = true
      try {
        const response = await axios.get(`/api/items?page=${this.page}`)
        this.items = [...this.items, ...response.data.items]
        this.hasMore = response.data.hasMore
        this.page++
      } finally {
        this.busy = false
      }
    }
  }
}

使用第三方组件库

Element UI分页组件示例:

<template>
  <el-pagination
    @current-change="handleCurrentChange"
    :current-page="currentPage"
    :page-size="pageSize"
    :total="total">
  </el-pagination>
</template>

脚本部分:

export default {
  data() {
    return {
      currentPage: 1,
      pageSize: 10,
      total: 0,
      items: []
    }
  },
  methods: {
    handleCurrentChange(val) {
      this.currentPage = val
      this.fetchData()
    },
    async fetchData() {
      const response = await axios.get(`/api/items?page=${this.currentPage}`)
      this.items = response.data.items
      this.total = response.data.total
    }
  }
}

性能优化建议

对于大数据量分页,考虑使用虚拟滚动技术。Vue-virtual-scroll-list是一个不错的选择:

npm install vue-virtual-scroll-list

实现示例:

vue实现分页加载

<template>
  <virtual-list :size="50" :remain="20" :data="items">
    <div v-for="item in items" :key="item.id">{{ item.name }}</div>
  </virtual-list>
</template>

分页加载的实现方式应根据具体业务场景选择,前端分页适合小数据量,后端分页适合大数据量,无限滚动适合移动端浏览体验。

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

相关文章

vue实现小说分页

vue实现小说分页

实现小说分页功能 Vue.js可以通过计算属性、动态渲染和分页逻辑实现小说分页功能。以下是具体实现方法: 数据准备 需要将小说内容存储为字符串或数组格式,建议按章节拆分: data() { r…

vue实现加载图片

vue实现加载图片

Vue 实现图片加载的方法 在 Vue 中加载图片可以通过多种方式实现,以下是一些常见的方法: 使用静态资源路径 将图片放在 public 或 assets 目录下,通过相对路径或绝对路径引用。…

vue实现分页按钮

vue实现分页按钮

实现分页按钮的基本思路 在Vue中实现分页按钮通常需要结合计算属性、事件绑定和动态样式。核心逻辑包括计算总页数、生成页码数组、处理页码切换事件以及高亮当前页码。 基础实现代码示例 <templ…

vue实现加载动画

vue实现加载动画

Vue 实现加载动画的方法 在 Vue 中实现加载动画可以通过多种方式完成,以下是一些常见的方法: 使用 CSS 动画 通过 CSS 定义动画效果,结合 Vue 的动态类绑定实现加载动画。以下是一个…

前端vue实现分页

前端vue实现分页

前端Vue实现分页的方法 在Vue中实现分页功能可以通过多种方式完成,以下是常见的实现方法: 使用Element UI的分页组件 Element UI提供了现成的分页组件el-pagination,…

vue实现数据分页

vue实现数据分页

Vue 实现数据分页的方法 使用计算属性分页 在 Vue 中,可以利用计算属性对数据进行分页处理。这种方法适用于前端分页,即一次性获取所有数据后在客户端进行分页。 data() { return…