user9962636
user9962636

Reputation:

Angular 6 : How to get router event even when navigated to same route

How to get router event even when navigated to same route, for examples I have three route

How can I catch event from Router while navigating to route1 from route 1, same route.

Upvotes: 0

Views: 3529

Answers (3)

Avinash Kumar
Avinash Kumar

Reputation: 11

You can solve this using any following way.

1) Just add this line following line in constructor

this.router.onSameUrlNavigation = 'reload';

this work with RouterModule.forRoot() and with RouterModule.forChild() both.

2) following line of code will only work with RouterModule.forRoot(), not with RouterModule.forChild(). Because RouterModule.forChild() accepts only one parameter with it.

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

    exports: [RouterModule]
})

export class AppRoutingModule {
}

Upvotes: 0

user9962636
user9962636

Reputation:

I got it, Thanks anyway, I used following config

@NgModule({
    imports: [RouterModule.forRoot(appRoutes, {onSameUrlNavigation: 'reload'})],
    exports: [RouterModule]
})
export class AppRoutingModule {

}

Upvotes: 0

user4676340
user4676340

Reputation:

Start by telling to reload the routing (i.e. replay the observable) :

this.router.onSameUrlNavigation = 'reload';

Then, subscribe to route events (and optionally check for the event to be the last of the cycle, otherwise you will make a lot of things) :

this.router.events
  .pipe(filter(event => event instanceof NavigationEnd))
  .subscribe(event => {...});

Upvotes: 5

Related Questions