在实际开发项目中,有时我们会用到自定义按钮;因为一个项目中,众多的页面,为了统一风格,我们会重复用到很多相同或相似的按钮,这时候,自定义按钮组件就派上了大用场,我们把定义好的按钮组件导出,在全局引用,就可以在其他组件随意使用啦,这样可以大幅度的提高我们的工作效率。

好了,话不多说,上代码:
img-button.vue//这是我们自定义按钮组件

<template>
  <div class="img-button">
    <!-- 图片按钮 -->
    <div v-if="type === '查看'" :title="tag === '' ? type : tag" class="img-btn check-img"></div>
    <div v-if="type === '添加'" :title="tag === '' ? type : tag" class="img-btn add-img"></div>
    <div v-if="type === '修改'" :title="tag === '' ? type : tag" class="img-btn edit-img"></div>
    <div v-if="type === '删除'" :title="tag === '' ? type : tag" class="img-btn delete-img"></div>
    
    <div v-if="type === '刷新'" :title="tag === '' ? type : tag"  class="img-btn refresh-img"></div>
    <div v-if="type === '关闭'" :title="tag === '' ? type : tag" class="img-btn close-img"></div>
    
    <div v-if="type === '齿轮'" :title="tag === '' ? type : tag" class="img-btn gear-img"></div>
    <div v-if="type === '平面图'" :title="tag === '' ? type : tag" class="img-btn plan-img"></div>
    <div v-if="type === '地图'" :title="tag === '' ? type : tag" class="img-btn map-img"></div>
    <div v-if="type === '一般模板'" :title="tag === '' ? type : tag" class="img-btn normal-img"></div>
    <div v-if="type === '特殊模板'" :title="tag === '' ? type : tag" class="img-btn special-img"></div>
    <div v-if="type === '折线图'" :title="tag === '' ? type : tag" class="img-btn line-img"></div>
    <!-- 文字按钮 自定义大小-->
    <div v-if="type === '按钮'" :title="tag === '' ? name : tag" class="img-btn ibtn">{{name}}</div>
    <div v-if="type === '小按钮'" :title="tag === '' ? name : tag" class="ibtn-samll">{{name}}</div>
    <div v-if="type === '普通按钮'" :title="tag === '' ? name : tag" class="normal-btn">{{name}}</div>
  </div>
</template>

<script>
export default {
  name: 'ImgButton',
  props: { 
    type: {
      type: String,
      default: ''
    },
    name: {
      type: String,
      default: ''
    },
    tag: {
      type: String,
      default: ''
    }
  }
}
</script>

<style lang="less" scoped>
  .img-button {
    vertical-align: middle;
    display: inline-block;
    cursor: pointer;
    margin-right: 10px;
    .img-btn {
      .img-no-repeat;
      width:25px;
      height:25px;
    }
    .img-btn:hover {
      transform: scale(1.1);
    }
    .refresh-img {
      background-image: url('../../assets/images/button/refresh.png');
    }
    .add-img {
      background-image: url('../../assets/images/button/add.png');
    }
    .delete-img {
      background-image: url('../../assets/images/button/delete.png');
    }
    .check-img {
      background-image: url('../../assets/images/button/check.png');
    }
    .close-img {
      background-image: url('../../assets/images/button/close.png');
    }
    .edit-img {
      background-image: url('../../assets/images/button/edit.png');
    }
    .gear-img {
      background-image: url('../../assets/images/button/gear.png')
    }
    .plan-img {
      background-image: url('../../assets/images/button/plan.png')
    }
    .map-img {
      background-image: url('../../assets/images/button/map.png')
    }
    .normal-img {
      background-image: url('../../assets/images/button/normal.png')
    }
    .special-img {
      background-image: url('../../assets/images/button/special.png')
    }
    .line-img {
      background-image: url('../../assets/images/button/line_chart.png')
    }
    .ibtn {
      width: auto;
      min-width: 100px;
      padding: 0 20px;
      font-size: 17px;
      height: 30px;
      line-height: 30px; 
      text-align: center;
      border-radius:15px;
      background-color: #2f5d98;
      vertical-align: middle;
      color:#00cccc;
    }
    .ibtn-samll {
      .ibtn;
      height: 25px;
      padding: 0 2px;
      font-size: 10px;
      line-height: 25px;
      border-radius: 0px;
      background-color: transparent;
      border: 1px solid #00cccc;
    }
    .ibtn-samll:hover {
      color: white;
      border: 1px solid white;
    }
    .normal-btn {
      .ibtn;
    }
    .normal-btn:hover {
      color: white;
      background-color: #ff9247;
    }
  }
</style>

在router.js中配置好路由
在main.js中引入

//注册自定义按钮  
import imgButton from './components/img-button'
Vue.use(imgButton)

然后就可以在其他组件使用了

<imgButton type='刷新' @click.native='refreshBtn'></imgButton>

//值得注意的是 自定义按钮组件添加点击事件的时候一定要加.native 因为.native 修饰符就是用来注册元素的原生事件而不是组件自定义事件的

好了 今天的分享就到这了。


哈哈哈
97 声望3 粉丝