【发布时间】:2021-11-10 21:52:14
【问题描述】:
简单的问题:
Angular 文档建议不要在 component 端进行条件渲染的情况下使用 ng-content:
如果您的组件需要有条件地渲染内容,或多次渲染内容,您应该将该组件配置为接受包含您要有条件地渲染的内容的元素。
不建议在这些情况下使用元素,因为当组件的使用者提供内容时,该内容始终会被初始化,即使组件没有定义元素或该元素位于 ngIf 语句中。 来自:https://angular.io/guide/content-projection#conditional-content-projection
这是否也适用于组件槽的消费者?例如:
<custom-table-component>
<div ng-content-select-directive *ngIf="condition">
This is a conditionally rendered element inhabiting an ng-content slot of a parent
component
</div>
</custom-table-component>
如果条件为假,底层的 ng-content-select 是否仍会被渲染?
【问题讨论】:
标签: html angular rendering slots ng-content