Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Angular 2 route to same component

I have upgraded Angular 2 from using the router marked "depricated" and started using the "new" router in rc5 and rc6. However now I am having an issue (started in rc5 and still the same in rc6) where having a component that needs to navigate to the same component but with different parameters (to fetch other data then the one that is in the view allready, based on the parameters).

In the depricated router, the constructor and AfterViewInit etc where called each time when navigating into the component with this.router.navigate. In the new router the constructor and other things does not get called when calling the same component again after one another. So I guess there is some kind of "magic" / caching happening. Also note that into the component I send one required parameter and a few optional parameters so the link looks something like this: http://localhost:2222/mycomponent/1;someotherparam=123

Is there any way to force the component to get created each time navigating to it?

like image 895
alexsoftware Avatar asked Sep 05 '16 17:09

alexsoftware


1 Answers

Take a look at the Rangle.io guide for Angular 2.

Specifically the section on Reading Route Parameters

The reason that the params property on ActivatedRoute is an Observable is that the router may not recreate the component when navigating to the same component. In this case the parameter may change without the component being recreated.

So your component will not be recreated but you can subscribe to changes in the parts of the route that you are interested in (params, data, fragment, queryParams) and call these initialization methods as your subscribe() callback.

like image 103
seangwright Avatar answered Jan 04 '23 02:01

seangwright