我是靠谱客的博主 震动秀发,这篇文章主要介绍Vue封装 分页组件,现在分享给大家,希望可以做个参考。

在这里插入图片描述

思路:
1.父子组件通过 props 传递相关参数
2.子组件改变父组件的参数通过 $emit 方法来触发
代码:

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
<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>
复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
<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>
复制代码
1
2
3
4
5
6
7
8
9
10
11
12
<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>

在父组件引用:

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
<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封装内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(80)

评论列表共有 0 条评论

立即
投稿
返回
顶部