前言
项目中需要动态修改一个 radio-group 组件的样式,便想到了 css 变量注入的方式。本文主要记录一下。
使用代码如下:
<template>
<div class="custom-tabs">
<a-radio-group v-model:value="value" button-style="solid">
<a-radio-button value="a">Hangzhou</a-radio-button>
<a-radio-button value="b">Shanghai</a-radio-button>
<a-radio-button value="c">Beijing</a-radio-button>
<a-radio-button value="d">Chengdu</a-radio-button>
</a-radio-group>
</div>
</template>
<script lang="ts">
import { defineComponent, ref } from 'vue'
export default defineComponent({
name: 'test',
setup() {
const value = ref<string>('a');
const tabsStyle = ref({
color: '#41464b',
activeColor: '#fff',
backgroundColor: '#409eff'
})
return {
value,
tabsStyle
}
},
})
</script>
<style lang="less" scoped>
.custom-tabs {
:deep(.ant-radio-group) {
.ant-radio-button-wrapper-checked:not(.ant-radio-button-wrapper-disabled) {
color: v-bind('tabsStyle.activeColor');
background: v-bind('tabsStyle.backgroundColor');
border-color: v-bind('tabsStyle.backgroundColor');
}
.ant-radio-button-wrapper {
color: v-bind('tabsStyle.color')
}
}
}
</style>