从零开始教你封装自己的vue组件

   组件(component)是vue.js最强大的功能之一,它可以实现功能的复用,以及对其他逻辑的解耦。但经过一段时间的使用,我发现自己并没有在业务中发挥出组件的最大价值。相信很多刚开始使用vue的朋友都和我有相同的状况,在日常的开发中虽然也使用组件,但不知不觉就将组件当成了减少业务代码长度,展现代码结构的一种工具,并没有做到可复用,发挥组件的长处。

  

  恰好,前几天有一个select下拉菜单的BUG,因为原生HTML不支持修改option的样式,所以就试着用div模拟select的同时,封装一个独立的组件,便于之后的复用。在做的过程中,也有些许感悟,所以总结一下拿出来和大家分享,希望对大家能有所帮助。

  

  想要封装好一个组件,一定要熟练掌握这三个技能,父组件 —> 子组件传值(props)、子组件 —> 父组件传值($emit)、以及插槽(slot)对于一个独立的组件来说,props是用来为组件内部注入核心的内容;$emit用来使这个独立的组件通过一些逻辑来融入其他组件中。举个具体点的例子,假如你要做一辆车,车轮是要封装的一个独立组件,props指的就是根据整个车的外形你可以给轮子设置一些你想要的且符合车风格的花纹,图案等;而$emit的作用则是让这些轮子能够和整辆车完美契合的运作起来。差不多就是这个意思,下面来看代码。

 

  首先,我们先完成div的模拟代码——

<template>
<div class="selectWrap">
  <div class="select-wrapper">
    <div class="select" @click = "triggerOption">
      <div class="select-content">{{selectContent.text}}</div>
      <div class="triangle-wrapper">
        <div id="triangle-down"></div>
      </div>
    </div>
    <div class="option-wrapper" style="display: none;">
      <div class="option-item" v-for = "(item,index) in subject" :key="index" @mouseout="out($event)" @mouseover="move($event)" @click = "choose(item)">{{item.text}}</div>
    </div>
  </div>
</div>
</template>
<script>
  export default{
    data(){
      return{
        selectContent:{value:0,text:"小张"}, //模拟select默认选中的值
        subject:[{value:0,text:"小张"},{value:1,text:"小李"}, //模拟option中的文本和value值
                   {value:2,text:"小王"},{value:4,text:"小明"}], 
      }
    },
    computed:{
      optionWrapper(){
        return document.querySelector(".option-wrapper");
      },
      selectCon(){
        return document.querySelector(".select-content");
      },
      subjectList(){
        return document.getElementsByClassName("option-item");
      },
    },
    methods:{
      move(event){ //模拟hover效果
        for(var item of this.subjectList){
          item.classList.remove("hover");
        }
        event.currentTarget.classList.add("hover");
      },
      out(event){
        event.currentTarget.classList.remove("hover");
      },
      triggerOption(){ //控制option的展示,以及选中后的高亮效果
        if (this.optionWrapper.style.display == "none") {
          this.optionWrapper.style.display = "block";
        }else{
          this.optionWrapper.style.display = "none";
        }
        for(var item of this.subjectList){
          if (item.innerHTML == this.selectContent.text) {
            item.classList.add("hover");
          }else{
            item.classList.remove("hover");
          }
        }
      },
      choose(item){ //选中“option”
        this.selectContent.text = item.text;
        this.optionWrapper.style.display = "none";
      }
    },
  }
</script>
<style>
  .selectWrap{ /*select的宽度*/
    width: 100px;
  }
  .select{
    position: relative;
    overflow: hidden;
    padding-right: 10px;
    min-width: 80px;
    width: 100%;
    height: 20px;
    line-height: 20px;
    border: 1px solid #000;
    cursor: default;
    font-size: 13px;
  }
  .select-content{
    text-align: left;
  }
  .triangle-wrapper{
    position: absolute;
    right: 0;
    top: 50%;
    transform: translateY(-50%);
    width: 18px;
    height: 20px;
    background-color: #fff;
    cursor: default;
  }
  #triangle-down{
    position: absolute;
    right: 5px;
    top: 50%;
    transform: translateY(-50%);
    width: 0;
    height: 0;
    border-left: 3px solid transparent;
    border-right: 3px solid transparent;
    border-top: 6px solid #000;
  }
  .option-wrapper{
    position: relative;
    overflow: hidden;
    min-width: 80px;
    width: 100%;
    border-right: 1px solid #000;
    border-bottom: 1px solid #000;
    border-left: 1px solid #000;
  }
  .option-item{
    min-width: 80px;
    height: 20px;
    line-height: 20px;
    padding-right: 10px;
    text-align: left;
    cursor: default; 
  }
  .hover{
    background-color: rgb(30,144,255);
    color:#fff !important;
  }
</style>    

 

  事实上,当你完成这段代码时,就已经完成了这一个组件。放到平时,我可能就直接把这段代码放到业务代码里直接用了。但既然是封装组件,我们肯定要把它抽出来了。首先我们先要思考一下,如果我们需要把这个组件抽出来,有哪些值需要父组件提供给我们呢???

  

  相信大家一眼就能看出来,subject和selectContent这两个data是需要父组件通过props传进来的。但还有别的吗?作为一个select,父组件如果只能控制内容是不是管的有点太少了?可不可以让父组件来管理select的宽度?高度?字体大小样式等等?答案是肯定的。父组件传的值越多,子组件的耦合就越低。下面,我们对代码进行微调——

<template>
   ......
</template>
<script>
  export default{
    props:["subject","selectContet","selectWidth”],
    mounted(){
      document.querySelector(".selectWrap").style.width = 
      this.selectWidth+"px";
    },
    computed:{
      optionWrapper(){
        return document.querySelector(".option-wrapper");
      },
      selectCon(){
        return document.querySelector(".select-content");
      },
      subjectList(){
        return document.getElementsByClassName("option-item");
      },
    },
    methods:{
      ......
      choose(item){
        this.selectContent.text = item.text;
        this.optionWrapper.style.display = "none";
      }
    },
  }
</script>
<style>
  /*.selectWrap{
    width: 100px;
  }*/
  .......
 </style>

  

  我们通过props将之前的subject和selectContent从父组件传了进来。同时,我们还将select的宽度传了进来,并通过mounted来设置宽度。这样,父组件就能控制子组件的内容和一些简单的样式了。当然,作为一个完善的组件,我们还需要为组件设置默认值,这样就算父组件不传值,我们的这个组件一样可以使用——

 

<template>
  ......
</template>
<script>
  export default{
    props:{
      selectWidth:{
        type:Number,
        default:100,
      },
      subject:{
        type:Array,
        default:function(){
          return []
        }
      },
      selectContent:{
        type:Object,
        default:function(){
          return {value:0,text:"请选择"}
        }
      },
    },
    mounted(){
      document.querySelector(".selectWrap").style.width = this.selectWidth+"px";
    },
    ...... 
    methods:{
      ......
      choose(item){
        this.selectContent.text = item.text;
        this.optionWrapper.style.display = "none";
      }
    },
  }
</script>
<style>
  ......
</style>

 

  这回我们将props用对象的方式声明,并设置了默认值(default),假如父组件没有设置子组件的宽度,那么我们可以使用默认的100px。这样,我们的组件更加的完善!当然,我们的组件还有一个关键的功能没有实现,就是把选中的值传回给父组件,不然的话这个组件就没有意义了,我们来看choose这个函数——

choose(item,value){
    this.selectContent.text = item.text;
    this.optionWrapper.style.display = "none";
    this.$emit("changeSelect",this.selectContent.text,this.selectContent.value);
}

 

这样,我们就可以把选到的文本和value值传给父组件了。

当然,这仅仅是一个开头,字体大小等内容我还没有设置,不过这个组件现在已经完全可以拿出去用了。如果你也需要这个组件,欢迎来我的github上下载——

https://github.com/saitoChen/vue-select

 

posted @ 2017-10-21 17:24  手撕黄油  阅读(37635)  评论(0编辑  收藏  举报