vue中怎样完成的自定义按钮

在现实开辟项目中,偶然我们会用到自定义按钮;由于一个项目中,浩瀚的页面,为了一致作风,我们会反复用到许多雷同或类似的按钮,这时刻,自定义按钮组件就派上了大用场,我们把定义好的按钮组件导出,在全局援用,就能够在其他组件随便运用啦,如许能够大幅度的进步我们的工作效率。

好了,话不多说,上代码:
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 修饰符就是用来注册元素的原生事宜而不是组件自定义事宜的

好了 本日的分享就到这了。

    原文作者:哈哈哈
    原文地址: https://segmentfault.com/a/1190000018688150
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞