有没有办法在Angular2组件中的ng-content中使用css?

前端之家收集整理的这篇文章主要介绍了有没有办法在Angular2组件中的ng-content中使用css?前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我尝试通过ng-content包含组件中包含的css for children元素.它似乎尚未在Angular 2中实现,或者除了将css放在一般样式表中之外,有人可能有解决方案?

app.component.ts

<comp-parent>
  <comp-child></comp-child>
</comp-parent>

compParent.component.html

<div class="wrapper">
  <ng-content></ng-content>
</div>

compParent.component.css

.wrapper > comp-child {
   margin-right: 5px; <-- Not applied !!!
}

解决方法

您可以使用/ deep /(已弃用)或>>>或:: ng-deep选择器:

https://angular.io/guide/component-styles#deprecated-deep–and-ng-deep

例如.:

.wrapper ::ng-deep comp-child { ... }

请注意,>>>似乎不适用于基于angular-cli的项目.

@H_502_45@

猜你在找的Angularjs相关文章