且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

在新路由器上使用订阅功能时出现 Angular 2 打字稿错误 (rc 1)

更新时间: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())