Angular 2 route to the same component

I updated Angular 2 using a router labeled "stripped" and started using the "new" router in rc5 and rc6. However, now I have a problem (running in rc5 and still the same in rc6) where there is a component that should go to the same component but with different parameters (to get other data, i.e. the one that is in the view allready, based on the parameters).

In the devoid router, constructor and AfterViewInit, etc., which is called every time when navigating the component with this.router.navigate. In the new router, the constructor and other things are not called when the same component is called again one after another. Therefore, I assume some kind of β€œmagic” / caching is happening. Also note that I send one required parameter and several optional parameters to the component, so the link looks something like this: http: // localhost: 2222 / mycomponent / 1; someotherparam = 123

Is there a way to get a component to get it every time it transitions?

+5
source share
1 answer

Check out the Rangle.io guide for Angular 2.

In particular, the section Reading Route Parameters

The reason that the params property in ActivatedRoute is observable is because the router cannot recreate the component when navigating to the same component. In this case, the parameter may change without recreating the component.

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

+5
source

All Articles