vue父组件中无法修改子组件的css样式

阅读 65

2022-02-18

有时候我们想要在父组件中修改子组件中的样式,发现无法修改,是因为我们父组件的style标签中添加了scoped属性,当前页面的样式只作用到了当前的组件,无法作用到子组件
父组件:

<template>
	<div class="box">
		<header />
	</div>
</template>
<style scoped>
	.el-card{
		background-color:pink;
	}
</style >

子组件:header.vue

<template>
	<p class="title">子组件中的内容</p>
</template>
<style scoped>
	.box{
		background-color:pink;
	}
	.title{
		font-size:25px;
		color:#aaa;
	}
</style >

第一种方法:我们可以去掉父组件style的scoped属性

<style>
	.box{
		background-color:pink;
	}
	.title{
		font-size:25px;
		color:#aaa;
	}
</style >

如果我们只想让class样式对子组件生效还可以给class加上 /deep/ 深度选择器
第二种方法:

当我们开启了深度选择器/deep/,里面的子类会自动开启深度选择器,即.title下的所有class默认都开启是深度选择器

精彩评论(0)

0 0 举报