您可能想在 header.component.html 和内容页面中使用<ng-content></ng-content>,您可以这样写:
...
<app-header>
<ul class="xy-content-page-header-menu">
<li><a routerLink="...">Content page specific link</a></li>
....
</ul>
</app-header>
...
另一方面,它允许您只使用一个可以合并到标题组件中的内容块。
如果要添加更多单独的块,则需要使用 ngTemplateOutlet (https://angular.io/api/common/NgTemplateOutlet) 或带有角度模板的 vcr.createEmbeddedView。示例 1:
<app-header [inputTemplate]="menu">
<ng-template #menu let-element>
<ul class="xy-content-page-header-menu">
<li><a routerLink="...">Content page specific link {{element.data}}</a></li>
....
</ul>
</ng-template>
</app-header>
在你的 header.component.html 中:
<ng-container *ngTemplateOutlet="inputTemplate, context: { $implicit: some_data_data_for_element_object_outside }"></ng-container>
示例 2(创建自定义结构指令 (https://angular.io/guide/structural-directives),以便您可以在 header.component.ts 中查询它,如果需要,您也可以在上一个示例中使用它):
<app-header>
<ul class="xy-content-page-header-menu" *myStructuralDirective="let element">
<li><a routerLink="...">Content page specific link {{element.data}}</a></li>
....
</ul>
</app-header>
所以你可以在你的 header.component.ts 中查询它并渲染到 DOM 中(你需要知道 ContentChild 和 ViewChild 是什么What's the difference between @ViewChild and @ContentChild?):
@Component...{
...
@ContentChild(MyStructuralDirective) menu:MyStructuralDirective;
@ViewChild('whereTheMenuGoes', {read: ViewContainerRef}) elementVcr: ViewContainerRef;
...
someRenderFunction(){
this.elementVcr.createEmbeddedView(menu._template, this.some_data_data_for_element_object_outside)
}
...
希望对你有帮助:)