更新时间:2022-10-17 14:22:23
新路由器
constructor(router:Router) {
router.events.subscribe(event:Event => {
if(event instanceof NavigationStart) {
}
// NavigationEnd
// NavigationCancel
// NavigationError
// RoutesRecognized
})
}
原创
Router
类有一个 EventEmitter
changes
你可以订阅:
The Router
class has an EventEmitter
changes
you can subscribe to:
ngOnInit(){
this._router.changes.subscribe(
next => {
if (!userIsLoggedInOrWhatever) {
this._router.navigate(['Login']);
}
}
)
}
关于如何获取之前的路由,请参见How检测 Angular 2 中的路线变化? (pairwise()
)
For how to get the previous route see How to detect a route change in Angular 2? (pairwise()
)