我在新的Angular 2路由器中看到这些新的事件.
Theres NavigationStart,NavigationEnd,NavigationFailed(或类似的东西)
有人知道如何使用这些吗?我已经搞砸了几件事,但是还是无法让他们做任何事情.
路由器提供可以订阅的事件observable
原文链接:/angularjs/140551.htmlconstructor(router:Router) { router.events.subscribe(event: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
import { Router,NavigationStart } from '@angular/router';
因为如果不导入它,instanceof将不起作用,并且未定义的NavigationStart错误将会上升.