Ответ 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
повысится.