最简单的复现就是三层嵌套组件,在第二层组件中给最底层的组件赋予样式,样式不生效
- 发布:2024-06-17 18:31
- 更新:2024-06-17 18:31
- 阅读:30
产品分类: uniapp/小程序/微信
PC开发环境操作系统: Windows
PC开发环境操作系统版本号: 11
第三方开发者工具版本号: 略
基础库版本号: 1.06
项目创建方式: CLI
CLI版本号: 3.0.0-alpha-4010920240607001
操作步骤:
预期结果:
样式能够生效
样式能够生效
实际结果:
样式不生效
样式不生效
bug描述:
子组件给其他组件赋予样式异常。示例为三部分,index.vue、aaa.vue、m-button.vue。
//index.vue
<template>
<view class="container">
<view style="width: 100rpx; height: 100rpx">
<aaa></aaa>
</view>
</view>
</template>
<script setup lang="ts">
import aaa from "./components/aaa.vue";
</script>
<style lang="scss">
.container {
display: flex;
}
</style>
//aaa.vue
<template>
<view class="container11">
<MButton type="primary">你好</MButton>
</view>
</template>
<script setup lang="ts">
import MButton from "@/components/m-button.vue";
</script>
<style lang="scss">
.container11 {
&::v-deep .m-button {
width: 200rpx;
}
.m-button{
width: 200rpx;
}
}
</style>
//m-button.vue
<template>
<button
class="m-button"
:class="classComputed"
:open-type="openType"
@chooseavatar="emits('chooseavatar', $event)"
>
<text>
<slot></slot>
</text>
</button>
</template>
<script setup lang="ts">
const props = defineProps<{
type: "default" | "primary" | "gradient-primary";
size?: string;
disabled?: boolean;
loading?: boolean;
icon?: string;
round?: boolean;
openType?: "chooseAvatar";
}>();
const emits = defineEmits<{
(e: "chooseavatar", value: any): void;
}>();
const classComputed = computed(() => {
return {
"m-button--round": props.round,
"m-button--disabled": props.disabled,
"m-button--loading": props.loading,
[`m-button--${props.type}`]: props.type,
[`m-button--${props.size}`]: props.size,
};
});
</script>
<style lang="scss">
.m-button {
// 添加你需要的基础样式
}
.m-button--primary {
// 定义 primary 按钮的样式
}
.m-button--round {
// 定义圆角按钮的样式
}
// 添加其他需要的样式
</style>