1. 将此代码复制并粘贴到新组件中,例如“Pagination.vue”
<template>
<nav aria-label="...">
<ul class="pagination justify-content-center">
<li class="page-item" :class="{ disabled: pagination.current_page <= 1 }">
<a style="cursor:pointer" class="page-link" @click.prevent="changePage(1)" >First page</a>
</li>
<li class="page-item" :class="{ disabled: pagination.current_page <= 1 }">
<a style="cursor:pointer" class="page-link" @click.prevent="changePage(pagination.current_page - 1)"><i class="fa fa-arrow-left"></i></a>
</li>
<li class="page-item" v-for="(page,index) in pages" :key="page" :class="isCurrentPage(page) ? 'active' : ''">
<a style="cursor:pointer" class="page-link" @click.prevent="changePage(page)">{{ page }}
<span v-if="isCurrentPage(page)" class="sr-only">(current)</span>
</a>
</li>
<li class="page-item" :class="{ disabled: pagination.current_page >= pagination.last_page }">
<a style="cursor:pointer" class="page-link" @click.prevent="changePage(pagination.current_page + 1)"><i class="fa fa-arrow-right"></i></a>
</li>
<li class="page-item" :class="{ disabled: pagination.current_page >= pagination.last_page }">
<a style="cursor:pointer" class="page-link" @click.prevent="changePage(pagination.last_page)">Last Page</a>
</li>
</ul>
</nav>
</template>
<script>
export default {
props:['pagination', 'offset'],
methods: {
isCurrentPage(page){
return this.pagination.current_page === page
},
changePage(page) {
if (page > this.pagination.last_page) {
page = this.pagination.last_page;
}
this.pagination.current_page = page;
this.$emit('paginate');
}
},
computed: {
pages() {
let pages = []
let from = this.pagination.current_page - Math.floor(this.offset / 2)
if (from < 1) {
from = 1
}
let to = from + this.offset -1
if (to > this.pagination.last_page) {
to = this.pagination.last_page
}
while (from <= to) {
pages.push(from)
from++
}
return pages
}
}
}
</script>
js/app.js
2.在您的文件中使其成为全局,
Vue.component('pagination', require('./components/Pagination.vue').default);
3.在vue组件中,在数据下面,这样设置分页组件,你可以尽可能地改变偏移量
<pagination v-if="pagination.last_page > 1"
:pagination="pagination"
:offset="7"
@paginate="getItems()">
</pagination>
4.设置当前页面为1,
data(){
return{
items: [],
pagination: {
current_page: 1,
},
}
},
5. 制定发送页码和收集分页数据的方法,
getItems(){
axios.get('api/items?page='+this.pagination.current_page)
.then(response => {
this.items = response.data.data;
this.pagination = response.data.meta;
});
},
6.确保您返回带有资源集合的数据分页数据,
public function index(){
return new GeneralCollection(Item::with('category')->orderBy('name')->paginate(10));
}
***如果您没有收藏文件,请制作一个,例如“GeneralCollection”,
php artisan make:resource GeneralCollection
然后,将其包含在您要返回收集到的数据的控制器中,
use App\Http\Resources\GeneralCollection;
7. 恭喜!