Angular reloading route when route param changes

Angular reloading route when route param changes

Angular does not reload the component when the param in the route change.

For eg: when we have url like user/2 and when we change route to user/3, The route URL will be updated but page will not reload.

To resolve the issue you can simply add following line in the component constructor.

constructor(private router: Router) {

      // force route reload whenever params change;
      this.router.routeReuseStrategy.shouldReuseRoute = () => false;

}

This changes the routeReuseStrategy directly at the component level.

Another way is to use subscription method.Active route provides the query and route parameters in the form of observables.We can subscribe to those observables in the component and do required stuffs.

ngOnInit() {
	this.activeRoute.queryParams.subscribe(queryParams => {
		// do something with the query params
	}); 
}

One Reply to “Angular reloading route when route param changes”

Leave a Reply

Your email address will not be published. Required fields are marked *