Angular2:与路由子项的数据绑定 [英] Angular2: data binding with routing children
问题描述
我有一个简单的设置.我正在使用具有多个子组件的路由组件,这些子组件根据路由动态加载.我希望其中一些子组件与其路由父组件进行数据绑定.
I have a simple setup. I'm using a routing component with multiple child components which are loaded dynamically depending on the route. I'd like for some of these child components to have data-binding with its routing parent.
我将向子级添加一个 @Output() 属性,但是如何让路由父级侦听此事件发射器发出的事件?
I'll add an @Output() property to the child, but how can I make the routing parent listen for the events that this event-emitter emits?
通常在带有静态定义组件的模板中,我会这样做:
Normally in a template with a statically defined component I'll do something like this:
<child (myevent)="handleEvent($event)">
并且父级的 handleEvent 方法将处理该事件.
and the parent's handleEvent method will handle the event.
但是由于路由是动态插入组件的,所以这是不可能的.有没有办法让它工作?我知道我可以使用服务来实现相同的结果,但在这种情况下我想避免这种情况.
But since routing insert the components dynamically, this is not possible. Is there a way to make it work? I know I can use a service to achieve the same result, but I'd like to avoid that in this case.
谢谢
推荐答案
目前不支持.您可以使用共享服务与动态插入的子项共享数据.该服务可以使用 Observable
来允许孩子或父母订阅更改并获得主动通知.
This is currently not supported. You can use a shared service to share data with dynamically inserted children. The service can use Observable
to allow child or parent to subscribe to changes and get actively notified.
这篇关于Angular2:与路由子项的数据绑定的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!