简体   繁体   English

子组件的Angular 4访问子

[英]Angular 4 access child of child component

I'm using ngx-charts line chart component which has a timeline inside. 我正在使用内有时间表的ngx-charts折线图组件。 So the structure go like this : MyComponent -> LineChartComponent -> TimelineComponent. 因此结构如下所示:MyComponent-> LineChartComponent-> TimelineComponent。

(where LineChartComponent is the component from the library) (其中LineChartComponent是库中的组件)

Having a reference to LineChartComponent is it possible to execute something like @ViewChild() on this reference to access the TimelineComponent ? 有了LineChartComponent的引用,是否可以在此引用上执行类似@ViewChild()的操作来访问TimelineComponent?

If it's projected, you can use @ContentChild() , otherwise you need LineChartComponent to make it available. 如果已投影,则可以使用@ContentChild() ,否则需要LineChartComponent使其可用。

The better approach would be to use a shared service that you provide at MyComponent and inject to MyComponent and TimelineComponent and use like a message bus to communicate between these two components for example using Observables. 更好的方法是使用您在MyComponent提供的共享服务,并注入MyComponentTimelineComponent并像消息总线一样使用这两个组件在Observables之间进行通信。

See also https://angular.io/guide/component-interaction#parent-and-children-communicate-via-a-service 另请参阅https://angular.io/guide/component-interaction#parent-and-children-communicate-via-a-service

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

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