如何将数据注入从路由器创建的 Angular2 组件中? [英] How to inject data into Angular2 component created from a router?
问题描述
我目前正在尝试为我们的 Angular1 应用程序(相当复杂的应用程序)构建一个 Angular2 原型(基于 alpha44),并且我正在尝试在使用路由和子路由时找到最佳模型/数据架构.
I'm currently trying to build an Angular2 prototype (based on alpha44) of our Angular1 app (pretty complex one) and I'm trying to find the best model/data architecture when using routes and child routes.
在我的示例中,从路由创建的子组件中,我想访问父组件的属性(托管 router-outlet
).
In my example, from a child component created from a route, I want to access a property of the parent component (hosting the router-outlet
).
但是当你从 router-outlet
创建组件时,你不能再使用 @Input
和 @Output
了.那么除了基本的 routeParams
和静态 routeData
之外,注入一些数据/属性的最佳实践是什么?
But when you create a component from a router-outlet
, you cannot use @Input
and @Output
anymore.
So what is the best practice to inject some data/properties, except basic routeParams
and static routeData
?
如何在没有过多耦合的情况下与父组件通信?
How do you communicate with the parent component without too much coupling?
推荐答案
共享服务可以与路由器添加的组件一起使用.有关详细信息,请参阅 https://angular.io/docs/ts/latest/cookbook/component-communication.html
A shared service can be used with components added by the router. For details see https://angular.io/docs/ts/latest/cookbook/component-communication.html
data
支持也被添加到 RC.4 中的新路由器中.有关详细信息,请参阅 我如何传递数据在 Angular 2 组件中使用路由?
data
support also was added to the new router in RC.4. For details see How do I pass data in Angular 2 components while using Routing?
Angular 2 - 相当于新路由器的路由器解析数据也可能是相关的.
这篇关于如何将数据注入从路由器创建的 Angular2 组件中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!