在Vue.js中实现移动端分页,通常涉及到以下几个基础概念:
在Vue.js中实现移动端分页,可以使用第三方库如vue-pagination-2
或v-pagination
,也可以自己编写逻辑。
vue-pagination-2
)npm install vue-pagination-2
<template>
<div>
<ul>
<li v-for="item in paginatedData" :key="item.id">{{ item.name }}</li>
</ul>
<pagination :records="totalRecords" :per-page="perPage" @paginate="fetchData" />
</div>
</template>
<script>
import Pagination from 'vue-pagination-2';
export default {
components: {
Pagination
},
data() {
return {
items: [], // 所有数据
paginatedData: [], // 当前页数据
totalRecords: 0,
perPage: 10,
currentPage: 1
};
},
methods: {
fetchData(page) {
// 模拟获取数据
this.paginatedData = this.items.slice((page - 1) * this.perPage, page * this.perPage);
this.totalRecords = this.items.length;
}
},
mounted() {
// 初始化数据
this.items = Array.from({ length: 100 }, (_, i) => ({ id: i, name: `Item ${i}` }));
this.fetchData(this.currentPage);
}
};
</script>
vue-virtual-scroller
。vue-virtual-scroller
并使用它来优化长列表的渲染。通过以上方法,可以在Vue.js移动端应用中实现高效的分页功能。
领取专属 10元无门槛券
手把手带您无忧上云