vue实现列表分页
Vue 列表分页实现方法
基础分页实现
安装依赖(如使用第三方库)
npm install vue-paginate
模板部分示例
<template>
<div>
<ul>
<li v-for="item in paginatedData" :key="item.id">{{ item.name }}</li>
</ul>
<button @click="prevPage" :disabled="currentPage === 1">上一页</button>
<span>第 {{ currentPage }} 页</span>
<button @click="nextPage" :disabled="currentPage === pageCount">下一页</button>
</div>
</template>
脚本部分逻辑
export default {
data() {
return {
items: [], // 原始数据
currentPage: 1,
itemsPerPage: 10
}
},
computed: {
pageCount() {
return Math.ceil(this.items.length / this.itemsPerPage)
},
paginatedData() {
const start = (this.currentPage - 1) * this.itemsPerPage
const end = start + this.itemsPerPage
return this.items.slice(start, end)
}
},
methods: {
nextPage() {
if (this.currentPage < this.pageCount) {
this.currentPage++
}
},
prevPage() {
if (this.currentPage > 1) {
this.currentPage--
}
}
}
}
使用Element UI分页组件
安装Element UI
npm install element-ui
模板示例

<template>
<div>
<el-table :data="tableData">
<!-- 表格列定义 -->
</el-table>
<el-pagination
@current-change="handleCurrentChange"
:current-page="currentPage"
:page-size="pageSize"
:total="total"
layout="prev, pager, next">
</el-pagination>
</div>
</template>
脚本逻辑
export default {
data() {
return {
tableData: [],
currentPage: 1,
pageSize: 10,
total: 0
}
},
methods: {
fetchData() {
// 模拟API请求
const start = (this.currentPage - 1) * this.pageSize
const end = start + this.pageSize
this.tableData = mockData.slice(start, end)
this.total = mockData.length
},
handleCurrentChange(val) {
this.currentPage = val
this.fetchData()
}
},
created() {
this.fetchData()
}
}
服务器端分页实现
API请求示例
methods: {
async fetchPaginatedData() {
const response = await axios.get('/api/items', {
params: {
page: this.currentPage,
limit: this.itemsPerPage
}
})
this.items = response.data.items
this.totalItems = response.data.total
}
}
分页参数处理

watch: {
currentPage(newVal) {
this.fetchPaginatedData()
}
}
分页样式优化
添加CSS样式
.pagination {
display: flex;
justify-content: center;
margin-top: 20px;
}
.page-item {
margin: 0 5px;
cursor: pointer;
}
.active {
font-weight: bold;
color: #409EFF;
}
高级功能实现
添加页码跳转
<input v-model.number="goToPage" type="number" min="1" :max="pageCount">
<button @click="goToSpecificPage">跳转</button>
对应方法
methods: {
goToSpecificPage() {
if (this.goToPage >= 1 && this.goToPage <= this.pageCount) {
this.currentPage = this.goToPage
}
}
}






