繁体   English   中英

如何在Angular 2中获得子组件的contentchildren?

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

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

@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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM