如何获取子组件的内容Angular 2中的子组件

How to get a child component's contentchildren in Angular 2?

本文关键字:组件 Angular 何获取 获取      更新时间:2023-09-26

我在 Angular 2 中遇到了问题。我有一个显示菜单的主组件。此组件有一个名为 Tab 的子组件。在此组件中,有一些 Tab 组件是在主组件中单击菜单项时动态添加的。我使用选项卡组件中的@ContentChildren来获取所有选项卡组件,这工作正常。但是,要使其正常工作,我需要将这些内容子项进一步传递到主组件中。我该怎么做?我试过发射,但它似乎不起作用。

@ContentChildren(TabComponent) tabs: QueryList<TabComponent>;
@Output() public tabsArrayStart = new EventEmitter();
this.tabsArrayStart.emit(this.tabs);

@Output()只允许从父级传递

(tabsArrayStart)="doSomething($event)

我建议你改用共享服务。

有关更多详细信息,请参阅 https://angular.io/docs/ts/latest/cookbook/component-communication.html#!#bidirectional-service