vue实现换页
Vue 实现分页功能
在 Vue 中实现分页功能可以通过多种方式完成,以下是几种常见的实现方法。
使用第三方库
使用现成的分页组件库可以快速实现分页功能,例如 element-ui 或 vant。
安装 element-ui:

npm install element-ui
在 Vue 项目中引入并使用分页组件:
<template>
<el-pagination
@size-change="handleSizeChange"
@current-change="handleCurrentChange"
:current-page="currentPage"
:page-sizes="[10, 20, 30, 40]"
:page-size="pageSize"
layout="total, sizes, prev, pager, next, jumper"
: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>
自定义分页组件
如果需要更灵活的分页逻辑,可以自定义分页组件。

创建分页组件 Pagination.vue:
<template>
<div class="pagination">
<button @click="prevPage" :disabled="currentPage === 1">上一页</button>
<span>{{ currentPage }} / {{ totalPages }}</span>
<button @click="nextPage" :disabled="currentPage === totalPages">下一页</button>
</div>
</template>
<script>
export default {
props: {
totalItems: {
type: Number,
required: true
},
itemsPerPage: {
type: Number,
default: 10
}
},
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>
<style>
.pagination {
display: flex;
justify-content: center;
align-items: center;
gap: 10px;
}
</style>
在父组件中使用自定义分页组件:
<template>
<div>
<ul>
<li v-for="item in paginatedItems" :key="item.id">{{ item.name }}</li>
</ul>
<Pagination
:total-items="items.length"
:items-per-page="itemsPerPage"
@page-changed="handlePageChange"
/>
</div>
</template>
<script>
import Pagination from './Pagination.vue';
export default {
components: { Pagination },
data() {
return {
items: [], // 假设这是从 API 获取的数据
itemsPerPage: 5,
currentPage: 1
}
},
computed: {
paginatedItems() {
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>
结合后端分页
在实际项目中,通常会结合后端 API 实现分页,前端只负责传递页码和每页数量。
<template>
<div>
<ul>
<li v-for="item in items" :key="item.id">{{ item.name }}</li>
</ul>
<Pagination
:total-items="totalItems"
:items-per-page="itemsPerPage"
@page-changed="fetchData"
/>
</div>
</template>
<script>
import Pagination from './Pagination.vue';
export default {
components: { Pagination },
data() {
return {
items: [],
totalItems: 0,
itemsPerPage: 10,
currentPage: 1
}
},
created() {
this.fetchData();
},
methods: {
async fetchData(page = 1) {
this.currentPage = page;
const response = await axios.get('/api/items', {
params: {
page: this.currentPage,
limit: this.itemsPerPage
}
});
this.items = response.data.items;
this.totalItems = response.data.total;
}
}
}
</script>
通过以上方法,可以根据项目需求选择合适的方式实现 Vue 分页功能。






