Angular 2 typescript error when using subscribe function on new router (rc 1)

前端 未结 1 1110
我在风中等你
我在风中等你 2020-12-11 20:16

I am trying to set up authentication for my Angular 2 app with the new router. Someone suggested to try the following:

constructor (private _router: Router)          


        
相关标签:
1条回答
  • 2020-12-11 20:35

    new router

    constructor(router:Router) {
      router.events.subscribe(event:Event => {
        if(event instanceof NavigationStart) {
        }
        // NavigationEnd
        // NavigationCancel
        // NavigationError
        // RoutesRecognized
      })
    }
    

    original

    The Router class has an EventEmitter changes you can subscribe to:

    ngOnInit(){
      this._router.changes.subscribe(
        next => {
          if (!userIsLoggedInOrWhatever) {
            this._router.navigate(['Login']);
          }
        }
      )    
    }
    

    For how to get the previous route see How to detect a route change in Angular 2? (pairwise())

    0 讨论(0)
提交回复
热议问题