How to inject data into Angular2 component created from a router?

删除回忆录丶 提交于 2019-12-19 06:51:23

问题


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.

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).

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?


回答1:


You can use RouteData in order to pass data into routes. See here and here. I'm still missing the part of initialising this data obj from the component (see my question regarding)




回答2:


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 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 - equivalent to router resolve data for new router might also be related.




回答3:


You can pass (inject) data from child component inside Router Outlet to Parent component with Subject,Observable. You can found my solution in this video.

See the code on GitHub: https://github.com/tabvn/angular-blog



来源:https://stackoverflow.com/questions/33308340/how-to-inject-data-into-angular2-component-created-from-a-router

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!