溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

vue制作自定義按鈕的方法

發(fā)布時(shí)間:2020-05-12 10:43:21 來(lái)源:億速云 閱讀:299 作者:Leah 欄目:web開(kāi)發(fā)

本篇文章主要探討vue制作自定義按鈕的方法的解決方法。有一定的參考價(jià)值,有需要的朋友可以參考一下,跟隨小編一起來(lái)看解決方法吧。

在實(shí)際開(kāi)發(fā)項(xiàng)目中,有時(shí)我們會(huì)用到自定義按鈕;因?yàn)橐粋€(gè)項(xiàng)目中,眾多的頁(yè)面,為了統(tǒng)一風(fēng)格,我們會(huì)重復(fù)用到很多相同或相似的按鈕,這時(shí)候,自定義按鈕組件就派上了大用場(chǎng),我們把定義好的按鈕組件導(dǎo)出,在全局引用,就可以在其他組件隨意使用啦,這樣可以大幅度的提高我們的工作效率。

好了,話不多說(shuō),上代碼:
img-button.vue//這是我們自定義按鈕組件

<template>
  <div>
    <!-- 圖片按鈕 -->
    <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 === '關(guān)閉'" :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">{{name}}</div>
    <div v-if="type === '普通按鈕'" :title="tag === '' ? name : tag">{{name}}</div>
  </div>
</template>

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

<style 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中引入

//注冊(cè)自定義按鈕  
import imgButton from './components/img-button'
Vue.use(imgButton)

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

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

//值得注意的是 自定義按鈕組件添加點(diǎn)擊事件的時(shí)候一定要加.native 因?yàn)?native 修飾符就是用來(lái)注冊(cè)元素的原生事件而不是組件自定義事件的

以上就是vue制作自定義按鈕的方法的具體操作,代碼詳細(xì)清楚,如果在日常工作遇到這個(gè)問(wèn)題,希望你能通過(guò)這篇文章解決問(wèn)題。如果想了解更多相關(guān)內(nèi)容,歡迎關(guān)注億速云行業(yè)資訊頻道!

向AI問(wèn)一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI