19

Recently I have heard about a property called onSameUrlNavigation where you can set it to "reload". I have googled about it but there are not many articles which shows the use of that property. Can somebody help me with a real time example of where exactly we need to use that property.

@ngModule({
 imports: [RouterModule.forRoot(routes, {onSameUrlNavigation: ‘reload’})],
 exports: [RouterModule],
 })
ark
  • 3,707
  • 5
  • 21
  • 36

5 Answers5

13

onSameUrlNavigation configures how the router handles navigation to the current URL. By default, the router will ignore this navigation. However, this prevents features such as a "refresh" button. Use this option to configure the behavior when navigating to the current URL. Default is 'ignore'.by setting it to ‘reload’ you can navigate the current rout and trigger the router events again by setting it to 'ignore' if you navigate to same page it won't change the rout, see this example:

imports: [ BrowserModule, FormsModule, RouterModule.forRoot(routes, {
    // onSameUrlNavigation: 'ignore',
    onSameUrlNavigation: 'reload'
  }) ],
Fateme Fazli
  • 11,582
  • 2
  • 31
  • 48
  • What is the use of triggering the router events again? – ark Jan 06 '19 at 14:12
  • 1
    @ark for e.g you want to refresh the page without changing the url and refresh the app, you rigger the NavigationEnd route event and for e.g re fetch the data or something else. – Fateme Fazli Jan 06 '19 at 14:23
  • 1
    Thank you. I got the gist of it :). https://medium.com/engineering-on-the-incline/reloading-current-route-on-click-angular-5-1a1bfc740ab2. This article is also a great example. – ark Jan 06 '19 at 14:47
13

For me, adding onSameUrlNavigation made absolutely no different to my Angular routing at all.

When trying to navigate from, say, /users:123 to /users:234, that option wouldn't allow me to refresh the page. My Angular pages have complex grids on them, and I specifically needed them to be disposed, and recreated, when I swap to a different user.

What did work (kill me now...) was to add this to my code.

let newRouterLink = '/users:123';
this.router.navigate(['/']).then(() => { this.router.navigate([newRouterLink ]); })

It's ugly, but it does the job....

Mike Gledhill
  • 27,846
  • 7
  • 149
  • 159
  • 3
    Mike, you may want to check out the `routeReuseStrategy` property of the `Router` class. Specifically, the `shouldReuseRoute` function of that property. It doesn't pertain to the original question so I won't go into it in more detail here, but I think it will accomplish what you are trying to do in a cleaner way. – Joe Newton Aug 10 '21 at 21:11
7

I followed Joe Newton's hint and ended up with:

  1. Add { onSameUrlNavigation: 'reload' } to @NgModule (most likely in app.module.ts) like this:

@NgModule({ imports: [RouterModule.forRoot(routes, { onSameUrlNavigation: 'reload' })] })

  1. In front of your this.router.navigate([PathName]) insert:

this.router.routeReuseStrategy.shouldReuseRoute = function() { return false; };

Tested on Angular 11.

wbartussek
  • 1,850
  • 1
  • 10
  • 8
  • this.router.routeReuseStrategy.shouldReuseRoute = function() { return false; }; was key for me, thanks so much for this! – Kris Boyd Oct 28 '21 at 16:11
2

Reloading Same Component by routerLink to Same Route in Angular

I would like to merge some of the information from some answers here into a single answer.

You can achieve reloading the same component from the same route by:

  1. Changing configuration in app-routing.module.ts
  2. Providing a RouteReuseStrategy (which returns false) in the component defining routerLink(s)

Example:

  1. app-routing.module.ts
//...
@NgModule({
  // See https://angular.io/api/router/Router#properties
  imports: [RouterModule.forRoot(routes, {
        // onSameUrlNavigation: 'ignore',  // default behavior
        onSameUrlNavigation: 'reload'
  })],
  exports: [RouterModule],
})
export class AppRoutingModule {}
  1. links.component.ts (arbitrary name, yours will be different)
export class LinksComponent implements OnInit {
// ...
  constructor(
    // ...
    private router: Router) { }

  ngOnInit(): void {
    //  ...
    // With the below line you can now reload the same component from the same route
    this.router.routeReuseStrategy.shouldReuseRoute = () => { return false; };
  }
}

Supporting Docs

How to handle a navigation request to the current URL. One of:

  • 'ignore' : The router ignores the request.
  • 'reload' : The router reloads the URL. Use to implement a "refresh" feature.

Note that this only configures whether the Route reprocesses the URL and triggers related action and events like redirects, guards, and resolvers. By default, the router re-uses a component instance when it re-navigates to the same component type without visiting a different component first. This behavior is configured by the RouteReuseStrategy. In order to reload routed components on same url navigation, you need to set onSameUrlNavigation to 'reload' and provide a RouteReuseStrategy which returns false for shouldReuseRoute.

Considerations:

  • In our usage we only used static URLs; we haven't tested with dynamic URLs in our approach.
  • This example was tested using Angular 12.
cody.codes
  • 1,384
  • 1
  • 18
  • 24
1

RouteReuseStrategy. In order to reload routed components on same url navigation, you need to set onSameUrlNavigation to 'reload' and provide a **RouteReuseStrategy which returns false** for shouldReuseRoute.

https://angular.io/api/router/Router

elciospy
  • 260
  • 4
  • 11