当前位置:首页 > VUE

vue 实现分页

2026-02-10 05:01:53VUE

实现分页的基本思路

在Vue中实现分页通常需要结合后端API返回的数据和前端的分页组件。核心逻辑包括计算总页数、处理当前页码变化以及动态加载数据。

分页组件封装

使用el-pagination(Element UI)或自定义分页组件:

<template>
  <el-pagination
    @current-change="handlePageChange"
    :current-page="currentPage"
    :page-size="pageSize"
    :total="total"
    layout="prev, pager, next">
  </el-pagination>
</template>

数据请求与分页参数

在Vue组件中定义分页相关数据和方法:

data() {
  return {
    currentPage: 1,
    pageSize: 10,
    total: 0,
    listData: []
  }
},
methods: {
  fetchData() {
    const params = {
      page: this.currentPage,
      size: this.pageSize
    }
    axios.get('/api/data', { params }).then(res => {
      this.listData = res.data.list
      this.total = res.data.total
    })
  },
  handlePageChange(val) {
    this.currentPage = val
    this.fetchData()
  }
}

前端分页实现(纯前端)

如果数据量较小,可在前端完成分页:

computed: {
  paginatedData() {
    const start = (this.currentPage - 1) * this.pageSize
    const end = start + this.pageSize
    return this.allData.slice(start, end)
  },
  totalPages() {
    return Math.ceil(this.allData.length / this.pageSize)
  }
}

样式优化建议

对于自定义分页组件,可添加CSS样式:

.pagination-container {
  display: flex;
  justify-content: center;
  margin-top: 20px;
}
.page-item {
  margin: 0 5px;
  cursor: pointer;
}
.active {
  font-weight: bold;
}

性能优化方案

对于大数据量分页:

vue 实现分页

  • 使用虚拟滚动技术(如vue-virtual-scroller)
  • 实现懒加载分页(滚动到底部自动加载)
  • 添加加载状态提示
  • 对页码切换进行防抖处理

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

相关文章

vue SSG实现

vue SSG实现

Vue SSG 实现方法 Vue 的静态站点生成(SSG)可以通过多种工具和框架实现,以下是几种常见的方法: 使用 VuePress VuePress 是一个基于 Vue 的静态站点生成器,适合文档…

vue实现mui

vue实现mui

使用 Vue 实现 MUI(Material-UI)风格的界面 Vue 本身不直接提供 MUI(Material Design)组件,但可以通过集成第三方库或手动实现来达到类似效果。以下是几种常见方法…

vue实现滚屏

vue实现滚屏

实现滚屏的基本方法 在Vue中实现滚屏效果可以通过多种方式完成,常见的有原生JavaScript滚动方法、第三方库或CSS动画。以下是几种常见实现方式: 使用window.scrollTo或Elem…

vue实现eventbus

vue实现eventbus

Vue 中实现 EventBus 在 Vue 中,EventBus 是一种跨组件通信的机制,尤其适用于非父子组件之间的数据传递。以下是实现 EventBus 的几种方法: 方法一:使用 Vue 实例…

vue app 实现

vue app 实现

Vue App 实现步骤 创建Vue项目 使用Vue CLI或Vite初始化项目。Vue CLI适合传统项目,Vite适合现代轻量级应用。 npm create vue@latest my-vu…

黑马vue实现

黑马vue实现

以下是关于使用 Vue.js 实现黑马相关功能或项目的关键方法和实践指南: 环境配置与项目初始化 安装最新版 Vue CLI 或使用 Vite 创建项目: npm create vue@lates…