当前位置:首页 > VUE

分页用vue实现

2026-01-08 16:55:00VUE

分页用 Vue 实现

在 Vue 中实现分页功能通常需要结合数据管理和 UI 组件。以下是几种常见的方法:

使用 Vue 组件库的分页组件

许多 Vue UI 组件库(如 Element UI、Vant、Ant Design Vue)提供了现成的分页组件。以 Element UI 为例:

分页用vue实现

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

<script>
export default {
  data() {
    return {
      currentPage: 1,
      pageSize: 10,
      total: 100
    }
  },
  methods: {
    handleCurrentChange(val) {
      this.currentPage = val
      this.fetchData()
    },
    fetchData() {
      // 根据 currentPage 和 pageSize 获取数据
    }
  }
}
</script>

自定义分页组件

如果需要完全自定义分页逻辑,可以手动实现:

分页用vue实现

<template>
  <div class="pagination">
    <button 
      @click="prevPage" 
      :disabled="currentPage === 1"
    >
      上一页
    </button>
    <span>第 {{ currentPage }} 页</span>
    <button 
      @click="nextPage" 
      :disabled="currentPage === totalPages"
    >
      下一页
    </button>
  </div>
</template>

<script>
export default {
  props: {
    totalItems: Number,
    itemsPerPage: Number
  },
  data() {
    return {
      currentPage: 1
    }
  },
  computed: {
    totalPages() {
      return Math.ceil(this.totalItems / this.itemsPerPage)
    }
  },
  methods: {
    prevPage() {
      if (this.currentPage > 1) {
        this.currentPage--
        this.$emit('page-changed', this.currentPage)
      }
    },
    nextPage() {
      if (this.currentPage < this.totalPages) {
        this.currentPage++
        this.$emit('page-changed', this.currentPage)
      }
    }
  }
}
</script>

结合后端 API 分页

实际项目中,分页通常需要与后端 API 配合:

<template>
  <div>
    <ul>
      <li v-for="item in items" :key="item.id">{{ item.name }}</li>
    </ul>
    <el-pagination
      :current-page="pagination.page"
      :page-size="pagination.limit"
      :total="pagination.total"
      @current-change="fetchData"
    />
  </div>
</template>

<script>
import axios from 'axios'

export default {
  data() {
    return {
      items: [],
      pagination: {
        page: 1,
        limit: 10,
        total: 0
      }
    }
  },
  created() {
    this.fetchData()
  },
  methods: {
    fetchData(page = 1) {
      this.pagination.page = page
      axios.get('/api/items', {
        params: {
          page: this.pagination.page,
          limit: this.pagination.limit
        }
      }).then(response => {
        this.items = response.data.items
        this.pagination.total = response.data.total
      })
    }
  }
}
</script>

前端数据分页

如果数据量不大,可以在前端完成分页:

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

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

关键点总结

  • 确定分页方式:前端分页还是后端分页
  • 计算总页数:Math.ceil(totalItems / itemsPerPage)
  • 处理页码变化事件
  • 根据当前页码获取对应数据
  • 考虑边界情况(第一页和最后一页)

根据项目需求选择合适的实现方式,组件库提供现成方案更快捷,自定义组件灵活性更高。

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

相关文章

vue实现选项卡分页

vue实现选项卡分页

Vue实现选项卡分页的方法 使用动态组件切换 通过Vue的<component :is="currentTab">动态加载不同组件,结合v-for循环渲染选项卡标题,点击时切换curre…

vue里实现增删改查

vue里实现增删改查

在Vue中实现增删改查 数据初始化 在Vue组件的data中定义初始数据和表单绑定的变量: data() { return { items: [ { id: 1, name:…

vue实现alert

vue实现alert

实现自定义 Alert 组件 在 Vue 中可以通过组件化方式实现自定义 Alert 弹窗。以下是基于 Vue 3 的实现示例: 组件代码 (Alert.vue) <template>…

vue实现流程

vue实现流程

Vue 实现流程的基本步骤 安装 Vue.js 可以通过 CDN 引入或使用 npm/yarn 安装。 CDN 方式: <script src="https://cdn.jsdelivr.…

vue实现socket

vue实现socket

Vue 中实现 WebSocket 通信 在 Vue 项目中实现 WebSocket 通信可以通过原生 WebSocket API 或第三方库(如 socket.io-client)完成。以下是两种常…

vue实现rtc

vue实现rtc

Vue 实现 RTC 的步骤 安装依赖 在 Vue 项目中安装 WebRTC 相关依赖,如 simple-peer 或 peerjs,用于简化 WebRTC 的实现。 运行以下命令安装 simple-…