angularroutes

Angular - Navigate to current same URL with component reload not the whole page


I am using Angular 5 library, I want to navigate to the current URL with reload (refresh) the whole component, not the page, I have read about navigation to current URL in Angular document.

And that it is what I need:

 /**
 * Define what the router should do if it receives a navigation request 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'.
 */
onSameUrlNavigation: 'reload' | 'ignore';

Now, I have tried to set the onSameUrlNavigation property to reload and then navigate to the same URL, but nothing happend, like this:

this.router.onSameUrlNavigation = 'reload';
this.router.navigate(['view'], { queryParams: { QueryUUID: selectedId } });

Anyone who has used this before can help?


Solution

  • I have found this workaround method since the onSameUrlNavigation property in Angular 5.1 is not what I need.

    I just need to override shouldReuseRoute, so added an exception for the component route that I want to refresh, if the current and future route are equal view.

    And also I can navigate to other routes without problems, even for navigate from Angular Material sidenav.

    this.router.routeReuseStrategy.shouldReuseRoute = function (future: ActivatedRouteSnapshot, curr: ActivatedRouteSnapshot) {
      if (future.url.toString() === 'view' && curr.url.toString() === future.url.toString()) {
        return false;
      }
      return (future.routeConfig === curr.routeConfig);
    };
    

    Update:

    For those they want to use the new onSameUrlNavigation property in Angular 5.1 you can refer to this blog:

    https://medium.com/engineering-on-the-incline/reloading-current-route-on-click-angular-5-1a1bfc740ab2


    Update:

    It is better to create a custom RouteReuseStrategy that override the default behavior, here it is how I do that:

    export class AppRouteReuseStrategy implements BaseRouteReuseStrategy {
    
      // all other methods will be the same as the default
    
      /*
      * Determines if a route should be reused.
      * This strategy returns `true` when the future route config and current route config are identical.
      * 
      * but we want to refresh the route if the data.refreshComponent is true in the route definition
      * */
      shouldReuseRoute(future: ActivatedRouteSnapshot, curr: ActivatedRouteSnapshot): boolean {
        // so we need to check if it is true. if not, we return the default behavior
        return future.data.refreshComponent ? false : future.routeConfig === curr.routeConfig;
      }
    }
    

    Then will add this as a provider in the AppModule:

    { provide: RouteReuseStrategy, useClass: AppRouteReuseStrategy }
    

    And the last thing to do is to mark which component you want to refresh in case of navigate to the same route, like this:

    const routes: Routes = [
      {
        path: 'view',
        component: ViewComponent,
        data: { refreshComponent: true },
      }
    ]