AHmedRef
AHmedRef

Reputation: 2611

Detect route change in All Angular2 components

i know how to detect route changes in each component, so i have to put the same code in each one to get this changes

but my purpose is to detect route changes in general way, is there any possibility to do this ?

Upvotes: 2

Views: 1756

Answers (1)

Alexander Ciesielski
Alexander Ciesielski

Reputation: 10824

You need to put your route detection logic in the top component, which has the topmost/first <router-outlet>.

In my case this would be AppComponent

.html

<router-outlet></router-outlet>

.ts

export class AppComponent {

   constructor(
       private router: Router
   ) { 
       router.events.subscribe(event => console.log(event));
   }
}

Upvotes: 2

Related Questions