Angular 从ReplaySubject获取布尔值<;布尔值>;可观测

Angular 从ReplaySubject获取布尔值<;布尔值>;可观测,angular,authentication,Angular,Authentication,我正在学习thinkster Angular2教程,我制作了这两个教程,用于检查用户是否经过身份验证: private isAuthenticatedSubject = new ReplaySubject<boolean>(1); public isAuthenticated = this.isAuthenticatedSubject.asObservable(); private isAuthenticatedSubject=新的ReplaySubject(1); public

我正在学习thinkster Angular2教程,我制作了这两个教程,用于检查用户是否经过身份验证:

private isAuthenticatedSubject = new ReplaySubject<boolean>(1);
public isAuthenticated = this.isAuthenticatedSubject.asObservable();
private isAuthenticatedSubject=新的ReplaySubject(1);
public isAuthenticated=this.isAuthenticatedSubject.asObservable();
现在,在我的auth-gaurd.service.ts中,我想检查isAuthenticated的布尔值。我该怎么做?我想这样做:

canActivate(
    route: ActivatedRouteSnapshot,
    state: RouterStateSnapshot
): Observable<boolean> {
    //THIS IS NOT WORKING! 
    //Operator '===' cannot be applied to types 'Observable<boolean>' and 'boolean'
    if (this.userService.isAuthenticated.take(1) === false) { 
        this.router.navigateByUrl('/login');
    }
    return this.userService.isAuthenticated.take(1);
}
canActivate(
    route: ActivatedRouteSnapshot,
    state: RouterStateSnapshot
): Observable<boolean> {

    var isAuthenticated = false;

    // Check if the user is authenticated
    this.userService.isAuthenticated.take(1).subscribe(function (data) {
        isAuthenticated = data;
    });

    // If the user is not authenticated, redirect to Login page
    if (!isAuthenticated) {
        this.router.navigateByUrl('/login');
    }

    return this.userService.isAuthenticated.take(1);
}
canActivate(
路由:ActivatedRouteSnapshot,
状态:RouterStateSnapshot
):可见{
//这不管用!
//运算符“==”不能应用于类型“Observable”和“boolean”
如果(this.userService.isAuthenticated.take(1)==false){
this.router.navigateByUrl('/login');
}
返回此.userService.isAuthenticated.take(1);
}
你可以从thinkster中找到完整的代码,正如我在评论中提到的,我通过订阅isAuthenticated解决了这个问题。代码应如下所示:

canActivate(
    route: ActivatedRouteSnapshot,
    state: RouterStateSnapshot
): Observable<boolean> {
    //THIS IS NOT WORKING! 
    //Operator '===' cannot be applied to types 'Observable<boolean>' and 'boolean'
    if (this.userService.isAuthenticated.take(1) === false) { 
        this.router.navigateByUrl('/login');
    }
    return this.userService.isAuthenticated.take(1);
}
canActivate(
    route: ActivatedRouteSnapshot,
    state: RouterStateSnapshot
): Observable<boolean> {

    var isAuthenticated = false;

    // Check if the user is authenticated
    this.userService.isAuthenticated.take(1).subscribe(function (data) {
        isAuthenticated = data;
    });

    // If the user is not authenticated, redirect to Login page
    if (!isAuthenticated) {
        this.router.navigateByUrl('/login');
    }

    return this.userService.isAuthenticated.take(1);
}
canActivate(
路由:ActivatedRouteSnapshot,
状态:RouterStateSnapshot
):可见{
var isAuthenticated=假;
//检查用户是否经过身份验证
this.userService.isAuthenticated.take(1).subscribe(函数(数据){
isAuthenticated=数据;
});
//如果用户未通过身份验证,请重定向到登录页面
如果(!已验证){
this.router.navigateByUrl('/login');
}
返回此.userService.isAuthenticated.take(1);
}
如评论中所述,我通过订阅isAuthenticated解决了这个问题。代码应如下所示:

canActivate(
    route: ActivatedRouteSnapshot,
    state: RouterStateSnapshot
): Observable<boolean> {
    //THIS IS NOT WORKING! 
    //Operator '===' cannot be applied to types 'Observable<boolean>' and 'boolean'
    if (this.userService.isAuthenticated.take(1) === false) { 
        this.router.navigateByUrl('/login');
    }
    return this.userService.isAuthenticated.take(1);
}
canActivate(
    route: ActivatedRouteSnapshot,
    state: RouterStateSnapshot
): Observable<boolean> {

    var isAuthenticated = false;

    // Check if the user is authenticated
    this.userService.isAuthenticated.take(1).subscribe(function (data) {
        isAuthenticated = data;
    });

    // If the user is not authenticated, redirect to Login page
    if (!isAuthenticated) {
        this.router.navigateByUrl('/login');
    }

    return this.userService.isAuthenticated.take(1);
}
canActivate(
路由:ActivatedRouteSnapshot,
状态:RouterStateSnapshot
):可见{
var isAuthenticated=假;
//检查用户是否经过身份验证
this.userService.isAuthenticated.take(1).subscribe(函数(数据){
isAuthenticated=数据;
});
//如果用户未通过身份验证,请重定向到登录页面
如果(!已验证){
this.router.navigateByUrl('/login');
}
返回此.userService.isAuthenticated.take(1);
}

看起来像是@GünterZöchbauer的复制品,问题是1)我使用了ReplaySubject而不是BehaviorSubject,因为我可以在没有初始值的情况下创建isAuthenticatedSubject,2)我想使用IsAuthenticate我还没有选中
ReplaySubject
也许它的工作原理与
BehaviorSubject
相同。你试过了吗?也许你可以让它共享,订阅它,并将价值存储在subscribe中的属性中。@GünterZöchbauer,谢谢,我是通过订阅像@GünterZöchbauer一样的isAuthenticatedLooks来实现的,问题是1)我使用了ReplaySubject而不是Behavior Subject,因为我可以创建没有初始值的isAuthenticatedSubject,2)我想使用IsAuthenticate我没有选中
ReplaySubject
,它的工作原理可能与
Behavior Subject
相同。你试过了吗?也许你可以让它共享,订阅它,并将价值存储在subscribe中的属性中。@GünterZöchbauer,谢谢,我是通过订阅isAuthenticated来实现的