vue如何实现分页显示
Vue实现分页显示的方法
使用Element UI的分页组件
Element UI提供了现成的分页组件el-pagination,适合快速集成。安装Element UI后,直接引入组件并绑定数据即可。

<template>
<el-pagination
@size-change="handleSizeChange"
@current-change="handleCurrentChange"
:current-page="currentPage"
:page-sizes="[10, 20, 30, 50]"
:page-size="pageSize"
layout="total, sizes, prev, pager, next"
:total="total">
</el-pagination>
</template>
<script>
export default {
data() {
return {
currentPage: 1,
pageSize: 10,
total: 100
};
},
methods: {
handleSizeChange(val) {
this.pageSize = val;
this.fetchData();
},
handleCurrentChange(val) {
this.currentPage = val;
this.fetchData();
},
fetchData() {
// 根据currentPage和pageSize请求数据
}
}
};
</script>
自定义分页逻辑
如果不依赖UI库,可以通过计算属性实现分页逻辑。假设有一个数据数组listData,通过slice方法截取当前页数据。

<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 >= totalPages">下一页</button>
</div>
</template>
<script>
export default {
data() {
return {
listData: [], // 原始数据
currentPage: 1,
pageSize: 5
};
},
computed: {
totalPages() {
return Math.ceil(this.listData.length / this.pageSize);
},
paginatedData() {
const start = (this.currentPage - 1) * this.pageSize;
const end = start + this.pageSize;
return this.listData.slice(start, end);
}
},
methods: {
prevPage() {
if (this.currentPage > 1) this.currentPage--;
},
nextPage() {
if (this.currentPage < this.totalPages) this.currentPage++;
}
}
};
</script>
结合后端API分页
实际项目中,分页通常需要后端配合。通过API传递page和pageSize参数,获取分页数据。
methods: {
async fetchData() {
const res = await axios.get('/api/data', {
params: {
page: this.currentPage,
pageSize: this.pageSize
}
});
this.listData = res.data.items;
this.total = res.data.total;
}
}
使用第三方库
如果需要更复杂的功能(如无限滚动),可以考虑以下库:
vuejs-paginate:轻量级分页组件v-pagination:简洁的风格化组件vue-awesome-paginate:支持自定义样式和动画
安装后按文档引入即可,逻辑与上述方法类似,主要差异在UI表现和配置选项上。






