首先引入框架和配置我就不說了,相信對你們來講不是問題,主要是怎麼把element的分頁組件封裝爲本身的須要注意的坑數組
廢話很少說,上代碼:框架
首先是引入的element分頁組件和配置屬性less
<template> <div class="pagination"> <el-pagination background :layout="layout" :pager-count="pagerCount" :page-sizes="pageSizes" :page-size="pageSize" :total="pageTotal" :current-page.sync="currentPages" @size-change="sizeChange" @current-change="current" @prev-click="prev" @next-click="next" ></el-pagination> </div> </template>
而後是傳遞的參數和默認值以及相關處理分頁函數,參數見說明函數
<script> export default { name: "HotelPaginationTemplate", props: { /*說明:若是下面的參數沒傳,就會按照默認值進行設置pageTotal,pageFunc函數必須傳和綁定, 若是layout設置了sizes(指定當前頁展現條數),則pageSize會按照指定的pageSizes數組裏面 的值來設置,指定的pageSize會被覆蓋*/ pageTotal: { type: Number, default: 1,//總頁數 }, pagerCount: { type: Number, default: 11,//若是頁數不少大概展現的頁碼 }, layout: { type: String, default: "total,sizes, prev, pager, next, jumper, ->, slot",//分頁組件會展現的功能項 }, pageSizes: { type: Array, default: () => { return [10, 20, 30, 40, 50, 100];//指定分頁展現條數 }, }, currentPage: { type: Number, default: 1,//指定跳轉到多少頁 }, pageSize: { type: Number, default: 10,//每頁展現的條數,根據本身實際,且會帶入請求 }, pageNum: { type: Number, default: 1,//第幾頁數據,根據本身實際,且會帶入請求 }, }, data() { return { pageData: { pageSize: this.pageSize, pageNum: this.pageNum, }, currentPages: this.currentPage, }; }, mounted() {}, methods: { //選擇每頁顯示數量 sizeChange(val) { this.pageData.pageSize = val; this.$emit("pageFunc", this.pageData); }, //選擇某一頁 current() { this.pageData.pageNum = this.currentPages; this.$emit("pageFunc", this.pageData); }, //上一頁 prev() { this.pageData.pageNum = this.pageData.pageNum - 1; this.$emit("pageFunc", this.pageData); }, //下一頁 next() { this.pageData.pageNum = this.pageData.pageNum + 1; this.$emit("pageFunc", this.pageData); }, }, }; </script>
相關樣式:this
<style lang="less" scoped> .pagination { width: 100%; padding:20px 0 20px 0; } </style>
實際引用:spa
<HotelPaginationTemplate :pageSize="pageSize" :layout="layout" :pageTotal="pageTotal" @pageFunc="pageFunc" ></HotelPaginationTemplate> import HotelPaginationTemplate from "@/components/HotelPaginationTemplate"; components: { HotelPaginationTemplate, }, data() { return { pageSize: 10, //每頁顯示條數 pageTotal: 1, //默認總條數 pageNum: 1, //實際當前頁碼 layout: "total,prev, pager, next, jumper, ->, slot", }; }, //分頁 pageFunc(data) { this.pageSize = data.pageSize; this.pageNum = data.pageNum; this.getTablelist();//請求數據的函數 }, 注意:請求成功以後給pageTotal賦值,若是遇到問題請留言