Ant Design Vue (antdv) Button 按鈕顏色自定義 這個(gè)有點(diǎn)麻煩,發(fā)布了最新的方法,請(qǐng)看最新發(fā)布的方法,很簡(jiǎn)易

請(qǐng)看最新的方法,比較合理與簡(jiǎn)潔 http://www.itdecent.cn/p/bfd786eb489b
今天使用了 antdv ,其它的感覺(jué)還好,就是按鈕是真的丑,而且顏色只有那么兩個(gè),有點(diǎn)受不了,就重新封裝了一下。需要的朋友可以直接拿去用

效果圖

效果圖

1. 在 src/components下創(chuàng)建一個(gè)公共組件: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. 將其注冊(cè)為公共組件(以前的文章有vue3自動(dòng)注冊(cè)公共組件的方法,這里就不再說(shuō)了)

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

<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('這個(gè)家伙真的會(huì)噴火')
    }
    return { test }
  }
}
</script>

<style scoped>
</style>

希望對(duì)需要的朋友有所幫助

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容