【发布时间】:2021-06-28 17:54:46
【问题描述】:
如何在 Angular 中设置封装子组件的样式?
以下是启用视图封装的子组件和父组件。
** child
//template
<div class="child-container"> </div>
// class
@Component({
selector: "app-child",
templateUrl: "./child.component.html",
styleUrls: ["./child.component.css"],
encapsulation: ViewEncapsulation.ShadowDom
})
** parent
//template
<div class="parent-container"> </div>
// class
@Component({
selector: "app-parent",
templateUrl: "./parent.component.html",
styleUrls: ["./parent.component.css"],
encapsulation: ViewEncapsulation.ShadowDom
})
如何从父组件的样式表中选择 Child 组件内的特定类并更改其样式?
** parent
//template
<div class="parent-container">
<app-child></app-child>
</div>
// class
@Component({
selector: "app-parent",
templateUrl: "./parent.component.html",
styleUrls: ["./parent.component.css"],
encapsulation: ViewEncapsulation.ShadowDom
})
//styles
app-child {
.child-container {
background colour: red;
}
}
目前,当我尝试从子组件中选择一个类时,我无法覆盖它的样式。请帮忙!
请记住,我用过的外壳需要 ViewEncapsulation.ShadowDom。所以我们必须牢记这一点。
【问题讨论】:
-
目前,当我尝试从子组件中选择一个类时,我无法覆盖它的样式。请帮忙!
标签: javascript angular