Passing objects to routing

I have a component in Angular2 that hosts a user table (userTableComponnent) and another userDetails component. When I click on a row in the users table, I want to redirect to userDetails. One implementation is to pass userId only, so in userDetails I get user details using another http get. However, this is redundant as I grab all user information in userTableComponent. So I really need to pass the User object from userTableComponent to userDetails. Any idea how to achieve this through routing?

+4
source share
2 answers

You can create the SessionService class and pass it through the application through Injection Dependency.

After selecting the user, you can attach the relative data to the SessionService instance introduced through the DI and get it in the UserDetails component.

I hope this helps

+9
source

Routing objects is rather limited. Using the service is the best option. If you provide a service instance with a parent component, then the same instance will be entered into the parent and child, and you will immediately get access to the shared data.

See also https://angular.io/docs/ts/latest/cookbook/component-communication.html#!#bidirectional-service

RC.4 data Angular 2 ?

+6

All Articles