Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use new NavigationStart @angular-router 3.0.0-alpha.*

I see these new events in the new Angular 2 Router.

Theres NavigationStart, NavigationEnd, NavigationFailed (or something like that)

Does anyone know how to use these yet? I've messed around with a few things but haven't been able to get them to do anything.

like image 345
ribsies Avatar asked Jun 22 '16 20:06

ribsies


People also ask

What is NavigationStart in angular?

The Angular Routers triggers several events starting with when the Navigation starts ( NavigationStart ) and also when the Navigation end ( NavigationEnd ) successfully. It is triggered when the navigation is canceled either by the user ( NavigationCancel ) or due to an error in the navigation ( NavigationError).

What is NavigationEnd in angular?

NavigationEndlinkAn event triggered when a navigation ends successfully. class NavigationEnd extends RouterEvent { constructor(id: number, url: string, urlAfterRedirects: string) type: EventType.


2 Answers

The Router provides an events observable that can be subscribed to

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

See also

  • https://angular.io/docs/ts/latest/api/router/index/Router-interface.html
  • https://angular.io/docs/ts/latest/api/router/index/NavigationStart-class.html
  • https://angular.io/docs/ts/latest/api/router/index/NavigationEnd-class.html
  • https://angular.io/docs/ts/latest/api/router/index/NavigationCancel-class.html
  • https://angular.io/docs/ts/latest/api/router/index/NavigationError-class.html
  • https://angular.io/docs/ts/latest/api/router/index/RoutesRecognized-class.html

NOTE

don't forget to import NavigationStart from router module

import { Router, NavigationStart } from '@angular/router'; 

because if you don't import it instanceof will not work and an error NavigationStart is not defined will rise.

like image 181
Günter Zöchbauer Avatar answered Sep 21 '22 18:09

Günter Zöchbauer


Just like this

 constructor(   private router:Router  ){}  this.router.events   .filter(event=> event instanceof NavigationStart)   .subscribe((event:NavigationStart)=>{      // TODO   }); 
like image 39
yo yo Avatar answered Sep 18 '22 18:09

yo yo