如何使用新的NavigationStart @ angular-router 3.0.0-alpha.*
问题描述:
我在新的Angular 2 Router中看到了这些新事件.
I see these new events in the new Angular 2 Router.
有NavigationStart,NavigationEnd,NavigationFailed(或类似的东西)
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.
答
Router
提供了一个可订阅的events
可观察对象
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
}
});
另请参见
- 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
- 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
注意
不要忘记从router
模块导入NavigationStart
import { Router, NavigationStart } from '@angular/router';
因为如果不导入它,则instanceof
将不起作用,并且会出现错误NavigationStart is not defined
.
because if you don't import it instanceof
will not work and an error NavigationStart is not defined
will rise.