Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/email/3.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
Angular canActivate从服务获取更新的变量_Angular_Subscribe_Canactivate - Fatal编程技术网

Angular canActivate从服务获取更新的变量

Angular canActivate从服务获取更新的变量,angular,subscribe,canactivate,Angular,Subscribe,Canactivate,我有一个来自服务的post请求,如果用户登录就会返回,我想设置一个authGuard,使其仅在用户登录时显示管理面板。 我的职位要求如下: public isLoggedIn: boolean = false; checkSesh(data: object) { const options = { headers: new HttpHeaders().set('Content-Type', 'application/json; charset=utf-8'), w

我有一个来自服务的post请求,如果用户登录就会返回,我想设置一个authGuard,使其仅在用户登录时显示管理面板。 我的职位要求如下:

public isLoggedIn: boolean = false;
checkSesh(data: object) {
    const options = {
      headers: new HttpHeaders().set('Content-Type', 'application/json; charset=utf-8'),
      withCredentials: true
    }

    return this.http.post<any>(`${this.AUTH_SERVER}`, data, options).subscribe(
      (res) => {
        this.reload = res[0].reload;
        if (this.reload === 0) {
          this.isLoggedIn = true;
        }
      }
    );
  }
public isLoggedIn:boolean=false;
checkSesh(数据:对象){
常量选项={
headers:new-HttpHeaders().set('Content-Type','application/json;charset=utf-8'),
证书:正确
}
返回this.http.post(`${this.AUTH_SERVER}`,数据,选项)。订阅(
(res)=>{
this.reload=res[0]。重新加载;
如果(this.reload==0){
this.isLoggedIn=true;
}
}
);
}
我的authguard具有以下功能:

canActivate(
    next: ActivatedRouteSnapshot,
    state: RouterStateSnapshot): Observable<boolean | UrlTree> | Promise<boolean | UrlTree> | boolean | UrlTree {    
    return this.service.isLoggedIn;
  }
canActivate(
    next: ActivatedRouteSnapshot,
    state: RouterStateSnapshot): Observable<boolean | UrlTree> | Promise<boolean | UrlTree> | boolean | UrlTree {
    return this.service.checkSesh();
}
canActivate(
下一步:ActivatedRouteSnapshot,
状态:RouterStateSnashot):可观察的|承诺|布尔| UrlTree{
返回this.service.isLoggedIn;
}
但它返回false(在订阅之前获取布尔值)

在post请求运行且值已更新后,如何更新它


谢谢

您需要在canActivate函数中返回Observable作为结果。 因此,您可以将结果映射到checkSesh函数中,以返回true/false。 比如说:

checkSesh(data: object) {
    const options = {
        headers: new HttpHeaders().set('Content-Type', 'application/json; charset=utf-8'),
        withCredentials: true
    }

    return this.http.post<any>(`${this.AUTH_SERVER}`, data, options)
        .pipe(
            map((res) => {
                this.reload = res[0].reload;
                if (this.reload === 0) {
                    this.isLoggedIn = true;
                    return true;
                }
                return false;
            })
        );
}
checkSesh(数据:对象){
常量选项={
headers:new-HttpHeaders().set('Content-Type','application/json;charset=utf-8'),
证书:正确
}
返回this.http.post(`this.AUTH_SERVER}`,数据,选项)
.烟斗(
地图((分辨率)=>{
this.reload=res[0]。重新加载;
如果(this.reload==0){
this.isLoggedIn=true;
返回true;
}
返回false;
})
);
}
然后在canActivate函数中,您可以执行以下操作:

canActivate(
    next: ActivatedRouteSnapshot,
    state: RouterStateSnapshot): Observable<boolean | UrlTree> | Promise<boolean | UrlTree> | boolean | UrlTree {    
    return this.service.isLoggedIn;
  }
canActivate(
    next: ActivatedRouteSnapshot,
    state: RouterStateSnapshot): Observable<boolean | UrlTree> | Promise<boolean | UrlTree> | boolean | UrlTree {
    return this.service.checkSesh();
}
canActivate(
下一步:ActivatedRouteSnapshot,
状态:RouterStateSnashot):可观察的|承诺|布尔| UrlTree{
返回此.service.checkSesh();
}