概述
思路:
1.父子组件通过 props 传递相关参数
2.子组件改变父组件的参数通过 $emit 方法来触发
代码:
<div class="Pagination">
<div class="page-bar">
<ul>
<li><a v-on:click="goPage(cur-1)" :class="cur<=1 ? 'page-button-disabled':''">上一页</a></li>
<li v-for="index in indexs" v-bind:class="{ active: cur == index }" :key="index">
<a v-on:click="goPage(index)">{{ index<1 ? "..." : index }}</a>
</li>
<li><a v-on:click="goPage(cur+1)" :class="cur>=all ? 'page-button-disabled':''">下一页</a></li>
</ul>
</div>
</div>
<script>
export default {
name:'Pagination',
props: ['cur','all'],
computed: {
indexs: function () {
var left = 1;
var right = this.all;
var n = this.cur;
var ar = []
if (this.all >= 11) {
if (n > 5 && n < this.all - 4) {
left = n - 5
right = n + 4
} else {
if (n <= 5) {
left = 1
right = 10
} else {
right = this.all
left = this.all - 9
}
}
}
while (left <= right) {
ar.push(left)
left++
}
if (ar[0] > 1) {
ar[0] = 1;
ar[1] = -1;
}
if (ar[ar.length - 1] < this.all) {
ar[ar.length - 1] = this.all;
ar[ar.length - 2] = 0;
}
return ar
}
},
methods: {
//上 、下一页
goPage: function (index) {
if (index > this.all) return;
this.$emit('set-current-page', index);
this.$emit('btn-click', index)
},
}
}
</script>
<style scoped>
ul, li {margin: 0px;padding: 0px;}
.page-bar {-webkit-touch-callout: none;-webkit-user-select: none;-khtml-user-select: none;-moz-user-select: none;-ms-user-select: none;user-select: none;}
.page-button-disabled {color:#ddd !important;pointer-events: none;}
.page-bar li {list-style: none;display: inline-block;}
.page-bar li:first-child > a {margin-left: 0px;}
.page-bar a {border: 1px solid #ddd;text-decoration: none;position: relative;float: left;padding: 6px 12px;margin-left: -1px;line-height: 1.42857143;color: #42a032;cursor: pointer;}
.page-bar a:hover {background-color: #eee;}
.page-bar .active a {color: #fff;cursor: default;background-color: #1696a9;border-color: #1696a9;}
.page-bar i {font-style: normal;color: #d44950;margin: 0px 4px;font-size: 12px;}
</style>
在父组件引用:
<Pagination v-bind:cur="cur" v-bind:all="all" v-on:btn-click="listen" v-on:set-current-page="setCurrentPage" ></Pagination>
//初始值定义:
data(){
return {
cur: 1, //当前页
all: 12, //
}
}
//方法
listen: function (data) {
console.log('当前页码:' + data )
},
// 设置当前页码
setCurrentPage: function(index){
this.cur = index;
},
最后
以上就是震动秀发为你收集整理的Vue封装 分页组件的全部内容,希望文章能够帮你解决Vue封装 分页组件所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复