Как использовать новый NavigationStart @angular -router 3.0.0-alpha. * Я вижу эти новые события в новом маршрутизаторе Angular 2. Theres NavigationStart, NavigationEnd, NavigationFailed (или что-то в этом роде) Кто-нибудь знает, как их использовать? Я перепутал несколько вещей, но не смог заставить их ничего сделать. Ответ 1 Router предоставляет наблюдаемый events, который может быть подписан на constructor(router:Router) { router.events.subscribe(event => { if(event instanceof NavigationStart) { } // NavigationEnd // NavigationCancel // NavigationError // RoutesRecognized } }); См. также 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 Примечание не забудьте импортировать NavigationStart из Router module import { Router, NavigationStart } from '@angular/router'; потому что, если вы его не импортируете, instanceof не будет работать, а ошибка NavigationStart is not defined повысится. Ответ 2 Так же, как этот constructor( private router:Router ){} this.router.events .filter(event=> event instanceof NavigationStart) .subscribe((event:NavigationStart)=>{ // TODO });
Ответ 1 Router предоставляет наблюдаемый events, который может быть подписан на constructor(router:Router) { router.events.subscribe(event => { if(event instanceof NavigationStart) { } // NavigationEnd // NavigationCancel // NavigationError // RoutesRecognized } }); См. также 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 Примечание не забудьте импортировать NavigationStart из Router module import { Router, NavigationStart } from '@angular/router'; потому что, если вы его не импортируете, instanceof не будет работать, а ошибка NavigationStart is not defined повысится.
Ответ 2 Так же, как этот constructor( private router:Router ){} this.router.events .filter(event=> event instanceof NavigationStart) .subscribe((event:NavigationStart)=>{ // TODO });