1.新建pagination.js 引入el-pagination、el-select、el-input 组件然后封装一个全新的分页插件
<template>
<div class="pagination" v-if="conf.totalItems > 0">
<div class="pagination_total">共有 {{ conf.totalItems }} 条记录</div>
<div class="pagination_conf">
<el-pagination small layout="prev, pager, next" :current-page="conf.currentPage" :total="conf.totalItems" :page-size="conf.itemsPerPage" @prev-click="prev" @next-click="next" @current-change="changeCurrentPage"></el-pagination>
<el-select v-model="conf.itemsPerPage" @change="initCurrentPage()">
<el-option
v-for="item in itemsPageList"
:key="item.value"
:label="item.label"
:value="item.value">
</el-option>
</el-select>
<div class="jump">
跳至
<el-input v-model="jumpPage" @change="jumpTo"></el-input>
页
</div>
</div>
</div>
</template>
<script>
export default {
name: 'pagination',
props: {
conf: {
type: Object,
default(){
return {
currentPage: 1,
itemsPerPage: 10,
totalItems: 0,
}
}
},
itemsPageList: {
type: Array,
default(){
return [
{label:'10条/页',value:10},
{label:'20条/页',value:20},
{label:'50条/页',value:50},
{label:'100条/页',value:100},
]
}
}
},
data(){
return{
}
},
computed:{
numberOfPages(){
return Math.ceil(this.conf.totalItems / this.conf.itemsPerPage)
},
},
created() {
this.jumpPage = this.conf.currentPage;
},
methods:{
prev(num) {
if(this.conf.currentPage <= 1){
return
} else {
this.conf.currentPage = num === 'first' ? 1 : num
}
},
next(num) {
if(this.conf.currentPage >= this.numberOfPages){
return
} else {
this.conf.currentPage = num === 'end' ? 1 : num
}
},
changeCurrentPage(item){
this.conf.currentPage = item;
this.jumpPage = item;
},
initCurrentPage(){
this.conf.currentPage = 1;
this.jumpPage = 1;
},
jumpTo(){
if(Number(this.jumpPage<=0)){
this.jumpPage = 1;
}
let pagecount = Math.ceil(this.conf.totalItems / this.conf.itemsPerPage);
if(this.jumpPage==null || this.jumpPage<0 ||this.jumpPage>pagecount){
this.jumpPage = this.conf.currentPage
}else {
this.conf.currentPage = this.jumpPage;
this.changeCurrentPage(this.jumpPage);
}
}
}
}
</script>
<!-- Add "scoped" attribute to limit CSS to this component only -->
<style lang="less" scoped>
.pagination{
width: 100%;
height: 40px;
.pagination_total{
width: 160px;
height: 40px;
line-height: 40px;
float: left;
}
.pagination_conf{
width: calc(100% - 160px);
float: left;
/deep/ .el-pagination--small{
margin-top: 9px;
}
/deep/ .el-pagination {
width: 160px;
float: left;
}
/deep/ .el-select{
width: 100px;
float: left;
margin-left: 20px;
}
.jump{
float: left;
}
/deep/ .el-input{
width: 100px;
}
}
}
</style>
2.引入App.vue查看效果
<template>
<div id="app">
<img alt="Vue logo" src="./assets/logo.png">
<pagination :conf="listConf"></pagination>
</div>
</template>
<script>
import pagination from './components/pagination'
export default {
name: 'App',
components: {
pagination
},
data(){
return{
listConf:{
currentPage: 1,
itemsPerPage: 10,
totalItems: 50,
}
}
},
watch: {
'listConf.currentPage':{
handler:function (newValue, oldValue) {
if(newValue !== oldValue){
this.getList();
}
}
},
'listConf.itemsPerPage':{
handler:function (newValue, oldValue) {
if(newValue !== oldValue){
this.getList();
}
}
},
},
methods:{
getList(){
}
}
}
</script>
<style>
font-family: Avenir, Helvetica, Arial, sans-serif;
-webkit-font-smoothing: antialiased;
-moz-osx-font-smoothing: grayscale;
text-align: center;
color:
margin-top: 60px;
}
</style>

3.使用的时候监听页码和每页条数请求接口即可
watch: {
'listConf.currentPage':{
handler:function (newValue, oldValue) {
if(newValue !== oldValue){
this.getList();
}
}
},
'listConf.itemsPerPage':{
handler:function (newValue, oldValue) {
if(newValue !== oldValue){
this.getList();
}
}
},
},