如何在 Guard 运行之前将 isSignedIn 设置为 true?

How to set isSignedIn to true before Guard runs?

我正在使用 ngrx/router.

我有一个LoginGuard,当我打开一个需要登录的页面时,LoginGuard 在isSignedIn 设置为true 之前运行。所以当时isSignedIn就是undefined.

@Injectable()
export class LoginGuard implements Guard {
  constructor(private _router: Router, private _userService: UserService) { }

  protectRoute({ route, params, isTerminal }: TraversalCandidate): Observable<boolean> {
    return this._userService.checkSignedIn()
      .map(isSignedIn => {
        if (!isSignedIn) {
          this._router.replace('/landing');
          return false;
        } else {
          return true;
        }
      }).first();
  }
}

我把app开头的isSignedIn设为true,这是我印象最早的地方

class App implements OnInit {
  ngOnInit() {
    // I set isSignedIn in UserService to true here after I got user info from the server
  }
}

但也许还不够早?那么如何在 Guard 运行之前将 isSignedIn 设置为 true 呢?谢谢

我的应用程序实际上使用了一个以异步方式检查访问的服务。

@rtytgat 给出了解决方案。谢谢!

Check with the Guard, if not logged in send them to 'logging in' page, and send them back when login has been confirmed.