【问题标题】:*ngContainerOutlet style not working with ng-component encapsulation*ngContainerOutlet 样式不适用于 ng-component 封装
【发布时间】:2017-10-06 19:42:26
【问题描述】:

我使用*ngContainerOutlet动态加载组件,但是它将组件的模板封装在ng-component标签中,导致我的CSS规则失败。

例如:

<div class="my-class">
    <ng-container *ngComponentOutlet="MyComponent"></ng-container>
</div>

我最终得到了类似的东西:

<div class="my-class">
    <ng-component>
        <div>my content...</div>
    </ng-component>
</div>

导致my-class 无法应用于组件的模板。

我尝试创建一个类似my-class &gt; ng-component 的 CSS 规则,但由于它是动态创建的,所以它不起作用。与:first-child 相同。

有没有使用 CSS 或 Angular 的解决方案(例如,阻止这种封装)?

谢谢, 亚历山大

【问题讨论】:

    标签: css angular components


    【解决方案1】:

    更新

    ::slotted 现在被所有新浏览器支持并且可以与 `ViewEncapsulation.ShadowDom 一起使用

    https://developer.mozilla.org/en-US/docs/Web/CSS/::slotted

    原创

    您可以使用/deep/ 组合符来克服封装:

    :host /deep/ ng-component {
      ...
    }
    

    另见

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-03-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-03-19
    • 2021-12-27
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多