Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Angular 2 rc3 Router subscribe

Tags:

angular

router

In angular2 rc1 I subscribe for a change of route:

this.router.changes.subscribe(
() => {
    console.log(this.location.path());
});

How I can subscribe to change route in angular2 rc3? router.changes already doesn't exists.

like image 809
Denys Adamenko Avatar asked Jun 23 '16 09:06

Denys Adamenko


2 Answers

constructor(router:Router) {
  router.events.subscribe(event:Event => {
    if(event instanceof NavigationStart) {
    }
    // NavigationEnd
    // NavigationCancel
    // NavigationError
    // RoutesRecognized
  }
}

or

constructor(router:Router) {
  router.events.forEach(event:Event => {
like image 165
Günter Zöchbauer Avatar answered Oct 14 '22 11:10

Günter Zöchbauer


constructor(private router: Router) {
  this.router.events.subscribe(event => {
    if (event.constructor.name === 'NavigationStart') {
      console.log(event.url);
    }
  });
}
like image 3
andreasonny83 Avatar answered Oct 14 '22 12:10

andreasonny83