加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

vuejs2.0实现分页组件使用$emit进行事件监听数据传递的方法

发布时间:2020-12-17 03:03:47 所属栏目:百科 来源:网络整理
导读:文章介绍了vuejs实现的简单分页,如果我有几个页面都需要有分页效果,不可能每个页面都去复制一下这段代码吧,意思是封装一下,变成通用的组件。 首先使用基础 Vue 构造器,创建一个“子类”,Vue.extend( options ) '+ ' 1" '+ ' '+ ' '+ ' '+ ' '+ ' 共 {{a

文章介绍了vuejs实现的简单分页,如果我有几个页面都需要有分页效果,不可能每个页面都去复制一下这段代码吧,意思是封装一下,变成通用的组件。

首先使用基础 Vue 构造器,创建一个“子类”,Vue.extend( options )

'+ '
  • '+ '
  • '+ '
  • '+ '
  • '+ '
  • '+ '
  • {{all}}
  • '+ ''+ '
    '; var navBar = Vue.extend({ template:barHtml,props:['all','cur'],computed: { indexs: function(){ var left = 1; var right = this.all; var ar = []; if(this.all>= 5){ if(this.cur > 3 && this.cur < this.all-2){ left = this.cur - 2 right = this.cur + 2 }else{ if(this.cur<=3){ left = 1 right = 5 }else{ right = this.all left = this.all -4 } } } while (left <= right){ ar.push(left) left ++ } return ar } },methods: { btnclick: function(data){ if(data != this.cur){ this.cur = data; this.$emit('btn-click',data); } },pageClick: function(){ this.$emit('btn-click',this.cur); } },}); window.pagenav = navBar;

    这儿创建了一个全局的pagenav,可以在其它地方都可以调用。

    html代码

    css代码

    a { margin-left: 0px } .page-bar a{ display: block; border: 1px solid #ddd; text-decoration: none; position: relative; padding: 6px 12px; margin-left: -1px; line-height: 1.42857143; color: #337ab7; cursor: pointer } .page-bar a:hover{ background-color: #eee; } .page-bar a.banclick{ cursor:not-allowed; } .page-bar .active a{ color: #fff; cursor: default; background-color: #337ab7; border-color: #337ab7; } .page-bar i{ font-style:normal; color: #d44950; margin: 0px 4px; font-size: 12px; }

    新建一个vue对象实例

    简单的用js封装了一下分页组件。

    实现效果