vue前端实现分页
实现分页的基本思路
在Vue中实现分页功能通常需要结合后端API返回的分页数据,前端负责渲染分页控件并处理用户交互。分页组件通常包括页码按钮、上一页/下一页按钮以及总页数显示等元素。
分页组件代码示例
<template>
<div class="pagination">
<button @click="prevPage" :disabled="currentPage === 1">上一页</button>
<span v-for="page in pages" :key="page">
<button
@click="changePage(page)"
:class="{ active: currentPage === page }"
>
{{ page }}
</button>
</span>
<button @click="nextPage" :disabled="currentPage === totalPages">下一页</button>
</div>
</template>
<script>
export default {
props: {
totalItems: {
type: Number,
required: true
},
itemsPerPage: {
type: Number,
default: 10
},
currentPage: {
type: Number,
default: 1
}
},
computed: {
totalPages() {
return Math.ceil(this.totalItems / this.itemsPerPage)
},
pages() {
const range = []
for (let i = 1; i <= this.totalPages; i++) {
range.push(i)
}
return range
}
},
methods: {
changePage(page) {
this.$emit('page-changed', page)
},
prevPage() {
if (this.currentPage > 1) {
this.changePage(this.currentPage - 1)
}
},
nextPage() {
if (this.currentPage < this.totalPages) {
this.changePage(this.currentPage + 1)
}
}
}
}
</script>
<style>
.pagination {
display: flex;
justify-content: center;
margin-top: 20px;
}
.pagination button {
margin: 0 5px;
padding: 5px 10px;
}
.pagination button.active {
background-color: #42b983;
color: white;
}
</style>
使用分页组件
在父组件中使用分页组件并处理分页事件:

<template>
<div>
<!-- 数据列表 -->
<ul>
<li v-for="item in paginatedData" :key="item.id">
{{ item.name }}
</li>
</ul>
<!-- 分页组件 -->
<Pagination
:total-items="totalItems"
:items-per-page="itemsPerPage"
:current-page="currentPage"
@page-changed="handlePageChange"
/>
</div>
</template>
<script>
import Pagination from './Pagination.vue'
export default {
components: {
Pagination
},
data() {
return {
items: [], // 从API获取的所有数据
currentPage: 1,
itemsPerPage: 10
}
},
computed: {
totalItems() {
return this.items.length
},
paginatedData() {
const start = (this.currentPage - 1) * this.itemsPerPage
const end = start + this.itemsPerPage
return this.items.slice(start, end)
}
},
methods: {
handlePageChange(page) {
this.currentPage = page
}
}
}
</script>
优化分页显示
对于大量数据的分页,可以考虑只显示当前页附近的页码:

computed: {
pages() {
const range = []
const visiblePages = 5 // 显示的最大页码数
let start = Math.max(1, this.currentPage - Math.floor(visiblePages / 2))
let end = Math.min(this.totalPages, start + visiblePages - 1)
if (end - start + 1 < visiblePages) {
start = Math.max(1, end - visiblePages + 1)
}
for (let i = start; i <= end; i++) {
range.push(i)
}
return range
}
}
与后端API交互
实际项目中通常需要与后端API配合实现分页:
methods: {
async fetchData(page = 1) {
try {
const response = await axios.get('/api/items', {
params: {
page,
limit: this.itemsPerPage
}
})
this.items = response.data.items
this.totalItems = response.data.total
} catch (error) {
console.error(error)
}
},
handlePageChange(page) {
this.currentPage = page
this.fetchData(page)
}
}
分页样式优化
可以添加更多样式增强用户体验:
.pagination {
display: flex;
justify-content: center;
align-items: center;
margin: 20px 0;
}
.pagination button {
margin: 0 4px;
padding: 6px 12px;
border: 1px solid #ddd;
background: #fff;
cursor: pointer;
border-radius: 4px;
}
.pagination button:hover:not(:disabled) {
background: #f5f5f5;
}
.pagination button.active {
background: #42b983;
color: white;
border-color: #42b983;
}
.pagination button:disabled {
color: #ccc;
cursor: not-allowed;
}






