我在尝试在 vue3 中为父母的孩子设置样式时遇到很多问题。
在本例中,我创建了一个带有一些 css 属性的通用按钮,并尝试从其他组件自定义此按钮
家长
<template>
<OwnButton
class="accept-button"
@ownButtonClicked="emit('accept')"
>
<slot>
ACCEPT
</slot>
</OwnButton>
</template>
<script setup>
import OwnButton from 'path/to/own-button.vue';
const emit = defineEmits(['accept']);
</script>
<style scoped>
.accept-button :deep(.own-button)
{
background-color : #4CAF50 !important;
outline-color : green !important;
}
.accept-button :deep(.own-button:hover)
{
background-color: green !important;
}
</style>
Run Code Online (Sandbox Code Playgroud)
孩子
<template>
<button
class="own-button"
type="button"
@click="emit('ownButtonClicked')"
v-on:keyup.enter="emit('ownButtonClicked')"
>
<slot>
</slot>
</button>
</template>
<script setup>
const emit = defineEmits
([
'ownButtonClicked'
]);
</script>
<style scoped>
.own-button
{
background-color : azure;
outline-color …Run Code Online (Sandbox Code Playgroud)