vue实现网页分页
Vue实现网页分页的方法
使用Element UI的分页组件
Element UI提供了现成的分页组件el-pagination,可以快速实现分页功能。需要先安装Element UI库。
安装Element UI:
npm install element-ui
在Vue组件中使用:
<template>
<div>
<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>
</div>
</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() {
// 调用API获取数据
}
}
}
</script>
手动实现分页逻辑
如果不使用UI库,可以手动实现分页功能。以下是一个简单的实现示例:
<template>
<div>
<ul>
<li v-for="item in paginatedData" :key="item.id">{{ item.name }}</li>
</ul>
<div class="pagination">
<button @click="prevPage" :disabled="currentPage === 1">Previous</button>
<span>Page {{ currentPage }} of {{ totalPages }}</span>
<button @click="nextPage" :disabled="currentPage === totalPages">Next</button>
</div>
</div>
</template>
<script>
export default {
data() {
return {
data: [], // 原始数据
currentPage: 1,
itemsPerPage: 5
}
},
computed: {
totalPages() {
return Math.ceil(this.data.length / this.itemsPerPage);
},
paginatedData() {
const start = (this.currentPage - 1) * this.itemsPerPage;
const end = start + this.itemsPerPage;
return this.data.slice(start, end);
}
},
methods: {
prevPage() {
if (this.currentPage > 1) {
this.currentPage--;
}
},
nextPage() {
if (this.currentPage < this.totalPages) {
this.currentPage++;
}
}
},
created() {
// 获取数据
this.data = [...]; // 填充数据
}
}
</script>
使用Vue Paginate插件
Vue Paginate是一个轻量级的分页插件,可以简化分页实现。
安装Vue Paginate:
npm install vue-paginate
在Vue中使用:
<template>
<div>
<ul>
<li v-for="item in paginated('items')" :key="item.id">{{ item.name }}</li>
</ul>
<paginate
:page-count="totalPages"
:click-handler="changePage"
:prev-text="'Prev'"
:next-text="'Next'"
:container-class="'pagination'">
</paginate>
</div>
</template>
<script>
import Paginate from 'vue-paginate';
Vue.use(Paginate);
export default {
data() {
return {
items: [], // 数据
currentPage: 1,
itemsPerPage: 5
}
},
computed: {
totalPages() {
return Math.ceil(this.items.length / this.itemsPerPage);
}
},
methods: {
changePage(pageNum) {
this.currentPage = pageNum;
}
},
created() {
this.items = [...]; // 填充数据
}
}
</script>
结合后端API实现分页
在实际项目中,分页通常需要与后端API配合实现。以下是一个示例:

<template>
<div>
<el-pagination
@current-change="handlePageChange"
:current-page="currentPage"
:page-size="pageSize"
layout="prev, pager, next"
:total="total">
</el-pagination>
</div>
</template>
<script>
export default {
data() {
return {
currentPage: 1,
pageSize: 10,
total: 0,
list: []
}
},
methods: {
handlePageChange(page) {
this.currentPage = page;
this.fetchData();
},
fetchData() {
const params = {
page: this.currentPage,
size: this.pageSize
};
axios.get('/api/data', { params })
.then(response => {
this.list = response.data.list;
this.total = response.data.total;
});
}
},
created() {
this.fetchData();
}
}
</script>
以上方法涵盖了从简单到复杂的分页实现方案,可以根据项目需求选择适合的方式。






