vue实现分页加载
Vue 实现分页加载的方法
使用 Element UI 的分页组件
Element UI 提供了现成的分页组件 el-pagination,可以快速实现分页功能。
安装 Element UI:
npm install element-ui
在 Vue 文件中引入并使用:
<template>
<div>
<el-table :data="tableData">
<!-- 表格列定义 -->
</el-table>
<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 {
tableData: [],
currentPage: 1,
pageSize: 10,
total: 0
}
},
methods: {
handleSizeChange(val) {
this.pageSize = val;
this.fetchData();
},
handleCurrentChange(val) {
this.currentPage = val;
this.fetchData();
},
fetchData() {
// 模拟 API 调用
const start = (this.currentPage - 1) * this.pageSize;
const end = start + this.pageSize;
this.tableData = this.allData.slice(start, end);
this.total = this.allData.length;
}
},
mounted() {
this.fetchData();
}
}
</script>
自定义分页逻辑
如果不使用 UI 库,可以通过自定义逻辑实现分页。

定义分页数据和方法:
<template>
<div>
<ul>
<li v-for="item in paginatedData" :key="item.id">{{ item.name }}</li>
</ul>
<button @click="prevPage" :disabled="currentPage === 1">Previous</button>
<span>Page {{ currentPage }} of {{ totalPages }}</span>
<button @click="nextPage" :disabled="currentPage === totalPages">Next</button>
</div>
</template>
<script>
export default {
data() {
return {
allData: [], // 所有数据
paginatedData: [], // 当前页数据
currentPage: 1,
pageSize: 10,
totalPages: 0
}
},
methods: {
updatePaginatedData() {
const start = (this.currentPage - 1) * this.pageSize;
const end = start + this.pageSize;
this.paginatedData = this.allData.slice(start, end);
this.totalPages = Math.ceil(this.allData.length / this.pageSize);
},
prevPage() {
if (this.currentPage > 1) {
this.currentPage--;
this.updatePaginatedData();
}
},
nextPage() {
if (this.currentPage < this.totalPages) {
this.currentPage++;
this.updatePaginatedData();
}
}
},
mounted() {
// 模拟数据加载
this.allData = Array.from({ length: 100 }, (_, i) => ({ id: i, name: `Item ${i}` }));
this.updatePaginatedData();
}
}
</script>
结合后端 API 分页
实际项目中通常需要从后端 API 获取分页数据。
定义 API 调用方法:

<script>
export default {
data() {
return {
tableData: [],
currentPage: 1,
pageSize: 10,
total: 0
}
},
methods: {
async fetchData() {
try {
const response = await axios.get('/api/data', {
params: {
page: this.currentPage,
size: this.pageSize
}
});
this.tableData = response.data.items;
this.total = response.data.total;
} catch (error) {
console.error('Error fetching data:', error);
}
},
handlePageChange(page) {
this.currentPage = page;
this.fetchData();
}
},
mounted() {
this.fetchData();
}
}
</script>
使用无限滚动分页
对于移动端或需要滚动加载的场景,可以使用无限滚动。
安装 vue-infinite-scroll:
npm install vue-infinite-scroll
实现无限滚动:
<template>
<div v-infinite-scroll="loadMore" infinite-scroll-disabled="busy" infinite-scroll-distance="10">
<div v-for="item in items" :key="item.id">{{ item.name }}</div>
</div>
</template>
<script>
export default {
data() {
return {
items: [],
page: 1,
busy: false
}
},
methods: {
async loadMore() {
this.busy = true;
const newItems = await this.fetchData(this.page);
this.items = [...this.items, ...newItems];
this.page++;
this.busy = false;
},
async fetchData(page) {
const response = await axios.get(`/api/data?page=${page}`);
return response.data;
}
},
mounted() {
this.loadMore();
}
}
</script>
注意事项
- 分页逻辑需要与后端 API 的页码和每页大小参数保持一致。
- 对于大数据量,建议使用后端分页以减少前端性能压力。
- 无限滚动适合移动端或社交类应用,但需要注意内存管理。






