Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/google-apps-script/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript RXJS管道未侦听oberservable上的更改_Javascript_Angular_Rxjs_Angularfire2 - Fatal编程技术网

Javascript RXJS管道未侦听oberservable上的更改

Javascript RXJS管道未侦听oberservable上的更改,javascript,angular,rxjs,angularfire2,Javascript,Angular,Rxjs,Angularfire2,我正在尝试编写一个authGuard。这是我的代码: @Injectable() export class AuthGuard implements CanActivate { constructor(private auth: AuthService, private router: Router) {} canActivate(next: ActivatedRouteSnapshot, state: RouterStateSnapshot): Observable<boolean&

我正在尝试编写一个
authGuard
。这是我的代码:

@Injectable()
export class AuthGuard implements CanActivate {

constructor(private auth: AuthService, private router: Router) {}

canActivate(next: ActivatedRouteSnapshot, state: RouterStateSnapshot): Observable<boolean> {

    return this.auth.user.pipe(
        map(user => {
            if (user) {
                console.log('ok');
                return true;
            } else {
                console.log('denied');
                this.router.navigate(['/login']);
                return false;
            }
        })
    );
}
}
@Injectable()
导出类AuthGuard实现了CanActivate{
构造函数(私有身份验证:身份验证服务,私有路由器:路由器){}
canActivate(下一个:ActivatedRouteSnapshot,状态:RouterStateSnashot):可观察{
返回this.auth.user.pipe(
映射(用户=>{
如果(用户){
console.log('ok');
返回true;
}否则{
console.log('denied');
this.router.navigate(['/login']);
返回false;
}
})
);
}
}
在我的
authService
中,我有一个用户:
可见

问题是,
authGuard
仅在加载用户时检查一次是否为null。当guard返回true并允许访问页面时,一旦用户更改,它不会更新。因此,当我注销并且用户
oberservable
为空时,警卫应该返回false并重定向到登录页面,但这不起作用


因此,我可以制作
管道
来监听其
可观察的变化

您可以向我们展示您的路由配置吗?@PratapA.K当然,它很简单:
{path:'profile',component:ProfileComponent,canActivate:[AuthGuard]}
。在这个ProfileComponent上,我有一个注销按钮。@sychd那么,你是说我的管道操作将侦听防护外部的更改,而它无法侦听防护内部的更改?这听起来很奇怪……如果您的意图是实现注销功能,那么通常您会将用户重定向到从身份验证服务登录,而不是从防护内部登录。Auth-guard的工作是检查用户是否登录
AuthService
的实现是什么样子的?