简体   繁体   English

Angular 视图封装与外部库

[英]Angular view encapsulation with external libraries

I use an external component in my component.我在我的组件中使用了一个外部组件。 I want to do some styling on this external component.我想在这个外部组件上做一些样式。 Because of Angular View Encapsulation all css I define in my component are not propagated to this external component which is used in my html-template.由于 Angular View Encapsulation,我在组件中定义的所有 css 都不会传播到我的 html 模板中使用的这个外部组件。 In order to enable this propagation I set为了启用这种传播,我设置了

encapsulation: ViewEncapsulation.None

That works.那个有效。 But then my css is applied to my whole project not to a particular component only.但是后来我的 css 应用于我的整个项目,而不仅仅是应用于特定的组件。 What I need is to tell Angular somehow: "Apply this css to this component and all child components of it, also external ones".我需要以某种方式告诉 Angular:“将此 css 应用于此组件及其所有子组件,以及外部组件”。 Is that possible at all?这可能吗?

You can define this in your css.你可以在你的 css 中定义它。 However, be carefull as ::ng-deep is quite a powerfull combinator and can cause problems if not used correctly.但是,要小心,因为 ::ng-deep 是一个非常强大的组合器,如果使用不当可能会导致问题。 Also from the Angular website:同样来自 Angular 网站:

The shadow-piercing descendant combinator is deprecated and support is being removed from major browsers and tools.不推荐使用阴影穿透后代组合器,并且正在从主要浏览器和工具中删除支持。 As such we plan to drop support in Angular.因此,我们计划放弃对 Angular 的支持。

CSS: CSS:

 :host { color: red; }
 
 :host ::ng-deep parent {
   color:blue;
 }
 :host ::ng-deep child{
   color:orange;
 }
 :host ::ng-deep child.class1 {
   color:yellow;
 }
 :host ::ng-deep child.class2{
   color:pink;
 }

HTML: HTML:

  Angular2                                //red
  <parent>                                //blue
      <child></child>                     //orange
      <child class="class1"></child>      //yellow
      <child class="class2"></child>      //pink
  </parent>      

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

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