Angular2:带有路由子级的数据绑定 [英] Angular2: data binding with routing children

查看:37
本文介绍了Angular2:带有路由子级的数据绑定的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个简单的设置.我正在使用带有多个子组件的路由组件,这些子组件会根据路由动态加载.我希望其中一些子组件与其路由父组件具有数据绑定.

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆