Ant Design Vue (antdv) Button 按鈕顏色自定義 這個有點麻煩句葵,發(fā)布了最新的方法厕鹃,請看最新發(fā)布的方法,很簡易

請看最新的方法乍丈,比較合理與簡潔 http://www.reibang.com/p/bfd786eb489b
今天使用了 antdv 剂碴,其它的感覺還好,就是按鈕是真的丑诗赌,而且顏色只有那么兩個汗茄,有點受不了,就重新封裝了一下铭若。需要的朋友可以直接拿去用

效果圖

效果圖

1. 在 src/components下創(chuàng)建一個公共組件:CButton

<template>
  <a-button
    :type="customType"
    :class="customClass"
    :size="customSize"
    :disabled="disabled"
  >
    <template v-if="iconType" #icon>
      <component :is="iconType" />
    </template>
    <slot />
  </a-button>
</template>
<script>
import { defineComponent, ref, watch } from 'vue'
export default defineComponent({
  name: 'CButtonIndex',
  props: {
    type: { type: String, default: '' },
    size: { type: String, default: '' },
    icon: { type: String, default: '' },
    disabled: { type: Boolean, default: false },
    permission: { type: [String, Boolean], default: true }
  },
  setup (props) {
    const customClass = ref('c-button-primary')
    const customType = ref('')
    const customSize = ref('small')
    const iconType = ref('')
    watch(() => props.type, (v) => {
      switch (v) {
      case 'warning':
        customClass.value = 'c-button-warning'
        customType.value = 'default'
        break
      case 'error':
        customClass.value = 'c-button-error'
        customType.value = 'default'
        break
      case 'success':
        customClass.value = 'c-button-success'
        customType.value = 'default'
        break
      case 'primary':
        customClass.value = 'c-button-primary'
        customType.value = 'primary'
        break
      case 'cyan':
        customClass.value = 'c-button-cyan'
        customType.value = 'default'
        break
      case 'black':
        customClass.value = 'c-button-black'
        customType.value = 'default'
        break
      case 'purple':
        customClass.value = 'c-button-purple'
        customType.value = 'default'
        break
      case 'text':
        customClass.value = ''
        customType.value = 'text'
        break
      case 'link':
        customClass.value = ''
        customType.value = 'link'
        break
      default:
        customClass.value = ''
        customType.value = 'default'
        break
      }
    }, { immediate: true })
    watch(() => props.size, v => {
      customSize.value = !v ? 'small' : v
    }, { immediate: true })
    watch(() => props.icon, v => {
      iconType.value = v
    }, { immediate: true })
    watch(() => props, () => {}, { immediate: true })
    return {
      customClass,
      customType,
      customSize,
      iconType
    }
  }
})
</script>
<style scoped>
.c-button-primary {
  color: #fff;
  background-color: #2db7f5;
  border-color: #2db7f5;
}
.c-button-primary:hover {
  color: #fff;
  background-color: #3dc1fc;
  border-color: #2db7f5;
}
.c-button-primary[disabled], .c-button-primary[disabled]:hover, .c-button-primary[disabled]:focus, .c-button-primary[disabled]:active {
  color: rgba(0, 0, 0, 0.25);
  background: #f5f5f5;
  border-color: #d9d9d9;
  text-shadow: none;
  box-shadow: none;
}

.c-button-warning {
  color: #fff;
  background-color: #ff9900;
  border-color: #ff9900;
}
.c-button-warning:hover {
  color: #fff;
  background-color: #fcac35;
  border-color: #ff9900;
}
.c-button-warning[disabled], .c-button-warning[disabled]:hover, .c-button-warning[disabled]:focus, .c-button-warning[disabled]:active {
  color: rgba(0, 0, 0, 0.25);
  background: #f5f5f5;
  border-color: #d9d9d9;
  text-shadow: none;
  box-shadow: none;
}

.c-button-error {
  color: #fff;
  background-color: #ff3300;
  border-color: #ff3300;
}
.c-button-error:hover {
  color: #fff;
  background-color: #fc653f;
  border-color: #ff3300;
}
.c-button-error[disabled], .c-button-error[disabled]:hover, .c-button-error[disabled]:focus, .c-button-error[disabled]:active {
  color: rgba(0, 0, 0, 0.25);
  background: #f5f5f5;
  border-color: #d9d9d9;
  text-shadow: none;
  box-shadow: none;
}

.c-button-success {
  color: #fff;
  background-color: #00cc66;
  border-color: #00cc66;
}
.c-button-success:hover {
  color: #fff;
  background-color: #03e071;
  border-color: #00cc66;
}
.c-button-success[disabled], .c-button-success[disabled]:hover, .c-button-success[disabled]:focus, .c-button-success[disabled]:active {
  color: rgba(0, 0, 0, 0.25);
  background: #f5f5f5;
  border-color: #d9d9d9;
  text-shadow: none;
  box-shadow: none;
}

.c-button-cyan {
  color: #fff;
  background-color: #04c1e1;
  border-color: #04c1e1;
}
.c-button-cyan:hover {
  color: #fff;
  background-color: #0ad5f8;
  border-color: #04c1e1;
}
.c-button-cyan[disabled], .c-button-cyan[disabled]:hover, .c-button-cyan[disabled]:focus, .c-button-cyan[disabled]:active {
  color: rgba(0, 0, 0, 0.25);
  background: #f5f5f5;
  border-color: #d9d9d9;
  text-shadow: none;
  box-shadow: none;
}

.c-button-black {
  color: #fff;
  background-color: #131313;
  border-color: #131313;
}
.c-button-black:hover {
  color: #fff;
  background-color: #313131;
  border-color: #131313;
}
.c-button-black[disabled], .c-button-black[disabled]:hover, .c-button-black[disabled]:focus, .c-button-black[disabled]:active {
  color: rgba(0, 0, 0, 0.25);
  background: #f5f5f5;
  border-color: #d9d9d9;
  text-shadow: none;
  box-shadow: none;
}

.c-button-purple {
  color: #fff;
  background-color: #B500FE;
  border-color: #B500FE;
}
.c-button-purple:hover {
  color: #fff;
  background-color: #c951fa;
  border-color: #B500FE;
}
.c-button-purple[disabled], .c-button-purple[disabled]:hover, .c-button-purple[disabled]:focus, .c-button-purple[disabled]:active {
  color: rgba(0, 0, 0, 0.25);
  background: #f5f5f5;
  border-color: #d9d9d9;
  text-shadow: none;
  box-shadow: none;
}
</style>


2. 將其注冊為公共組件(以前的文章有vue3自動注冊公共組件的方法洪碳,這里就不再說了)

3. 使用方法(其它的原生屬性繼承添加可自動處理)

<template>
  <c-body>
    <div style="display: flex;flex-direction: column">
      <div style="display: flex;margin-bottom: 10px;">
        <span style="margin-right: 20px;">紫色:c-button type="purple"</span>
        <c-button type="purple" :loading="true">哈哈哈</c-button>
      </div>
      <div style="display: flex;margin-bottom: 10px;">
        <span style="margin-right: 20px;">黑色:c-button type="black"</span>
        <c-button type="black">哈哈哈</c-button>
      </div>
      <div style="display: flex;margin-bottom: 10px;">
        <span style="margin-right: 20px;">綠色:c-button type="success" icon="plus-outlined"</span>
        <c-button type="success" icon="plus-outlined" @click="test">哈哈哈</c-button>
      </div>
      <div style="display: flex;margin-bottom: 10px;">
        <span style="margin-right: 20px;">橙色:c-button type="warning"</span>
        <c-button type="warning">哈哈哈</c-button>
      </div>
      <div style="display: flex;margin-bottom: 10px;">
        <span style="margin-right: 20px;">紅色:c-button type="error"</span>
        <c-button type="error">哈哈哈</c-button>
      </div>
      <div style="display: flex;margin-bottom: 10px;">
        <span style="margin-right: 20px;">藍(lán)色:c-button type="primary"</span>
        <c-button type="primary">哈哈哈</c-button>
      </div>
      <div style="display: flex;margin-bottom: 10px;">
        <span style="margin-right: 20px;">Text:c-button type="text"</span>
        <c-button type="text">哈哈哈</c-button>
      </div>
      <div style="display: flex;margin-bottom: 10px;">
        <span style="margin-right: 20px;">Link:c-button type="link"</span>
        <c-button type="link">哈哈哈</c-button>
      </div>
    </div>
  </c-body>
</template>

<script>
export default {
  name: 'ViewsHome',
  setup () {
    const test= () => {
      console.log('這個家伙真的會噴火')
    }
    return { test }
  }
}
</script>

<style scoped>
</style>

希望對需要的朋友有所幫助

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個濱河市叼屠,隨后出現(xiàn)的幾起案子瞳腌,更是在濱河造成了極大的恐慌,老刑警劉巖镜雨,帶你破解...
    沈念sama閱讀 216,470評論 6 501
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件嫂侍,死亡現(xiàn)場離奇詭異,居然都是意外死亡荚坞,警方通過查閱死者的電腦和手機(jī)挑宠,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 92,393評論 3 392
  • 文/潘曉璐 我一進(jìn)店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來颓影,“玉大人各淀,你說我怎么就攤上這事」罟遥” “怎么了碎浇?”我有些...
    開封第一講書人閱讀 162,577評論 0 353
  • 文/不壞的土叔 我叫張陵,是天一觀的道長璃俗。 經(jīng)常有香客問我奴璃,道長,這世上最難降的妖魔是什么城豁? 我笑而不...
    開封第一講書人閱讀 58,176評論 1 292
  • 正文 為了忘掉前任苟穆,我火速辦了婚禮,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘鞭缭。我一直安慰自己剖膳,他們只是感情好魏颓,可當(dāng)我...
    茶點故事閱讀 67,189評論 6 388
  • 文/花漫 我一把揭開白布岭辣。 她就那樣靜靜地躺著,像睡著了一般甸饱。 火紅的嫁衣襯著肌膚如雪沦童。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 51,155評論 1 299
  • 那天叹话,我揣著相機(jī)與錄音偷遗,去河邊找鬼。 笑死驼壶,一個胖子當(dāng)著我的面吹牛氏豌,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播热凹,決...
    沈念sama閱讀 40,041評論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼泵喘,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了般妙?” 一聲冷哼從身側(cè)響起纪铺,我...
    開封第一講書人閱讀 38,903評論 0 274
  • 序言:老撾萬榮一對情侶失蹤,失蹤者是張志新(化名)和其女友劉穎碟渺,沒想到半個月后鲜锚,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 45,319評論 1 310
  • 正文 獨居荒郊野嶺守林人離奇死亡苫拍,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 37,539評論 2 332
  • 正文 我和宋清朗相戀三年芜繁,在試婚紗的時候發(fā)現(xiàn)自己被綠了。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片绒极。...
    茶點故事閱讀 39,703評論 1 348
  • 序言:一個原本活蹦亂跳的男人離奇死亡骏令,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出集峦,到底是詐尸還是另有隱情伏社,我是刑警寧澤,帶...
    沈念sama閱讀 35,417評論 5 343
  • 正文 年R本政府宣布塔淤,位于F島的核電站摘昌,受9級特大地震影響,放射性物質(zhì)發(fā)生泄漏高蜂。R本人自食惡果不足惜聪黎,卻給世界環(huán)境...
    茶點故事閱讀 41,013評論 3 325
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧稿饰,春花似錦锦秒、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,664評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至侣姆,卻和暖如春生真,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背捺宗。 一陣腳步聲響...
    開封第一講書人閱讀 32,818評論 1 269
  • 我被黑心中介騙來泰國打工柱蟀, 沒想到剛下飛機(jī)就差點兒被人妖公主榨干…… 1. 我叫王不留,地道東北人蚜厉。 一個月前我還...
    沈念sama閱讀 47,711評論 2 368
  • 正文 我出身青樓长已,卻偏偏與公主長得像,于是被迫代替她去往敵國和親昼牛。 傳聞我的和親對象是個殘疾皇子术瓮,可洞房花燭夜當(dāng)晚...
    茶點故事閱讀 44,601評論 2 353

推薦閱讀更多精彩內(nèi)容