在components文件夹中新建paging.vue文件
<template>
<div class="pagingBox">
<div class="pagItem"
@click="pageChange(1)">
{{isEn?'First':'首页'}}
</div>
<div class="pagItem"
@click="pageChange('pre')">
{{isEn?'Previous':'上一页'}}
</div>
<div v-if="pageList[2] - 2 > 1"
class="pagItem">...</div>
<div class="pagItem"
v-for="(item, index) in pageList"
:key="index"
:class="{ pagActive: page == item }"
@click="pageChange(item)">{{ item }}</div>
<div v-if="pageMax - pageList[2] > 2"
class="pagItem">...</div>
<div class="pagItem"
@click="pageChange('next')">
{{isEn?'Next':'下一页'}}
</div>
<div class="pagItem"
@click="pageChange(pageMax)">
{{isEn?'Last':'尾页'}}
</div>
<div class="pagTotal"
v-show="isTotal">
{{isEn?'Total '+total:'共 '+total+' 条'}}
</div>
</div>
</template>
<script>
export default {
props: {
total: {
type: Number,
default: 0
},
limit: {
type: Number,
default: 10
},
page: {
type: Number,
default: 1
},
isEn: {
type: Boolean,
default: false
},
isTotal: {
type: Boolean,
default: false
}
},
data () {
return {
pageList: []
};
},
computed: {
pageMax () {
return Math.ceil(this.total / this.limit);
}
},
created () {
this.initData();
},
methods: {
initData () {
this.pageList = [];
for (let i = 1; i <= this.pageMax; i++) {
if (i <= 5) this.pageList.push(i);
}
},
pageChange (pageCurrent) {
if (pageCurrent == 'pre') {
if (this.page > 1) {
this.$emit('page-change', this.page - 1);
} else {
this.$emit('page-change', this.page);
}
} else if (pageCurrent == 'next') {
if (this.page < this.pageMax) {
this.$emit('page-change', this.page + 1);
} else {
this.$emit('page-change', this.page);
}
} else {
this.$emit('page-change', pageCurrent);
}
}
},
watch: {
page (val) {
if (val <= 3) {
this.initData();
} else if (val === this.pageMax) {
this.pageList = [val - 2, val - 1, val];
} else if (val === this.pageMax - 1) {
this.pageList = [val - 2, val - 1, val, val + 1];
} else {
this.pageList = [val - 2, val - 1, val, val + 1, val + 2];
}
},
total () {
this.initData();
}
}
};
</script>
<style scoped>
.pagingBox {
margin-top: 35px;
margin-bottom: 50px;
display: flex;
justify-content: center;
align-items: center;
font-size: 16px;
line-height: 18px;
}
.pagItem {
padding: 7px 11px;
border: 1px solid #ddd;
color: #333;
margin-right: 10px;
cursor: pointer;
}
.pagActive {
background: #0b8686;
color: #fff;
border: 1px solid #0b8686;
}
.pagTotal {
font-size: 15px;
color: rgb(58, 58, 58);
margin-right: -40px;
margin-left: 20px;
}
</style>
在main.js中引入并注册paging组件
import paging from '../../components/paging'
Vue.component('paging', paging)
使用paging分页组件
<template>
<div>
<paging :total="this.paging.total"
:limit="this.paging.limit"
:page="this.paging.page"
isTotal
@page-change="pageChange"></paging>
</div>
</template>
<script>
export default {
data () {
return {
paging: {
total: 60,
limit: 8,
page: 1
}
};
},
methods: {
pageChange (page) {
if (page == this.paging.page) {
return;
}
this.paging.page = page;
}
}
};
</script>