Angular 2 - Routing - CanActivate work with Observable

前端 未结 7 1055
终归单人心
终归单人心 2020-11-30 18:30

I have an AuthGuard (used for routing) that implements CanActivate.

canActivate() {
    return this.loginService.isLoggedIn         


        
7条回答
  •  悲哀的现实
    2020-11-30 18:52

    in canActivate(), you can return a local boolean property (default to false in your case).

    private _canActivate: boolean = false;
    canActivate() {
      return this._canActivate;
    }
    

    And then in the result of the LoginService, you can modify the value of that property.

    //...
    this.loginService.login().subscribe(success => this._canActivate = true);
    

提交回复
热议问题