简体   繁体   English

将父组件 css 应用于不在 Angular 中的兄弟组件中的子组件 11

[英]Apply parent component css to child not in sibling component in Angular 11

  1. i have 2 types of component, i) before login ii) after login我有 2 种类型的组件,i) 登录前 ii) 登录后

  2. want to add 2 different css for both parent component,想为两个父组件添加 2 个不同的 css,

  3. with using ViewEncapsulation.None it apply in sibling component also, here is the structure of code使用ViewEncapsulation.None它也适用于兄弟组件,这是代码的结构

     - ParentComponent1 - ChildCompoent1.1 - ChildCompoent1.2 - ParentComponent2 - ChildCompoent2.1 - ChildCompoent2.2
  4. if i add CSS in ParentComponent1 then it only apply in ChildComponent1.1 and ChildComponent1.2 not in 2.1 and 2.2, same thing apply in ParentComponent2如果我在ParentComponent1中添加 CSS 那么它仅适用于 ChildComponent1.1 和 ChildComponent1.2 而不是 2.1 和 2.2,同样适用于ParentComponent2

is there any way to remove ViewEncapsulation.None or apply CSS only in child component not in Sibling component有没有办法删除ViewEncapsulation.None或仅在子组件中应用 CSS 而不是在兄弟组件中

You can use Emulated encapsulation您可以使用仿真封装

encapsulation: ViewEncapsulation.Emulated,封装:ViewEncapsulation.Emulated,

Similar to ViewEncapsulation.None, Angular adds the styles for this component to the head of the document, but with "scoped" styles.与 ViewEncapsulation.None 类似,Angular 将此组件的 styles 添加到文档的头部,但带有“范围”styles。

Therefore, only the elements directly within this component's template will match its styles.因此,只有直接在该组件模板中的元素才会匹配其 styles。 Since the "scoped" styles from the EmulatedEncapsulationComponent are very specific, they override the global styles from the NoEncapsulationComponent.由于 EmulatedEncapsulationComponent 中的“作用域”styles 非常具体,它们会覆盖 NoEncapsulationComponent 中的全局 styles。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM