Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

angular navigate to the same route with different parameter

Although this question seems to have popped up before, I can't find an answer that works for me, as the router seems to have changed a lot over the lifetime of angular.

In angular 5 I have a component where I wish to edit a user. I navigate to that component with the following code:

this.router.navigate(['editsingleuser',user.username]) 

Which will send me to /editsingleuser/bob

Then, from within that component I can also click on a button to edit my own user details, which uses the following code:

this.router.navigate(['editsingleuser',this.user.sub]) 

Which should send me to /editsingleuser/joe, but does not

Is there a parameter that I can add to the router.navigate that forces it to load the route, as it seems to be doing some kind of caching?

I have also tried using

[routerLink]="['editsingleuser',user?.sub]"  

which also has the same issue

like image 932
jazza1000 Avatar asked Dec 28 '17 11:12

jazza1000


People also ask

How does Angular routing handle route parameters?

To access the route parameters, we use route. snapshot , which is the ActivatedRouteSnapshot that contains information about the active route at that particular moment in time. The URL that matches the route provides the productId . Angular uses the productId to display the details for each unique product.

What is difference between routerLink and routerLink?

What is the difference between [routerLink] and routerLink ? How should you use each one? They're the same directive. You use the first one to pass a dynamic value, and the second one to pass a static path as a string.


2 Answers

I faced the same issue and this solved it in my case

constructor(private router: Router) {     this.router.routeReuseStrategy.shouldReuseRoute = function () {       return false;     };   } 
like image 85
Michael Meister Avatar answered Sep 27 '22 20:09

Michael Meister


In Angular 8, it is possible to load some other route and then return to the current active one. Just try this:

this.router.navigateByUrl('/', {skipLocationChange: true})   .then(()=>this.router.navigate(['editsingleuser',this.user.sub])); 

Credits:

  1. How to reload the current route with the angular 2 router
  2. https://dev.to/oleksandr/mastering-angular-8-five-things-that-are-good-to-know-to-save-your-time-14hk (Item #5: #5. Reload component on same URL navigation)
like image 43
Eduardo Vazquez Avatar answered Sep 27 '22 22:09

Eduardo Vazquez