Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/typescript/9.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 以角度将可观察的情况置于一种状态_Angular_Typescript_Rxjs_Observable_Guard - Fatal编程技术网

Angular 以角度将可观察的情况置于一种状态

Angular 以角度将可观察的情况置于一种状态,angular,typescript,rxjs,observable,guard,Angular,Typescript,Rxjs,Observable,Guard,我正试图建立一个基于api的登录系统, 我创建了3个函数: me():返回带有令牌的用户配置文件 isloggedin()返回一个布尔可观测值,基于:has the me()request data.success(已被api接受的令牌) 最后,我把它用在我的护角上 我有: console.log("triggered"); if(localStorage.getItem('token') && localStorage.getItem('logged') ==

我正试图建立一个基于api的登录系统, 我创建了3个函数: me():返回带有令牌的用户配置文件 isloggedin()返回一个布尔可观测值,基于:has the me()request data.success(已被api接受的令牌) 最后,我把它用在我的护角上

我有:

    console.log("triggered");
    if(localStorage.getItem('token') && localStorage.getItem('logged') == "True"){
      let meOptions = {
        headers: new HttpHeaders({
          'Content-Type':  'application/json',
          'Authorization': localStorage.getItem('token'),
        })
      };
    return this.http.get<user>('https://localhost:3000/user/me', meOptions);
  }else{
    return of(new user(false, "",""));
  }

  }
isloggedin():Observable<boolean>{
  return this.me().pipe(map(data=>{ //pipe sur undefined 
    console.log(data);
    if(data.success){
      console.log("renvoie oui")
      return true; 
    }else{
      console.log("renvoie non")
      return false;
    }
  }))
}
  • 返回true或false 但是,当它进入可观察状态时:

你把你的可观察物交给守卫,让他们处理订阅

return this.auth.isLoggedIn().pipe(
  tap(loggedIn => {
    if (!loggedIn) {
      this.router.navigate(['auth']);
    }
  })
)

这不是一件大事,如果你的if声明你是在接近可观察的,而不是价值。如果出现以下情况,请将其替换为:

if(this.auth.isloggedin().value){
  console.log("You logged - guard")
  return true;

}
编辑1:

对不起,我使用这个答案是希望有一个主题,而不是一个可观察的。那样的话,你真的必须订阅

编辑2:

在这种情况下,您可以只订阅该可观察对象,并将其值分配给类中的属性,然后使用该属性而不是该可观察对象生成if语句,如:

bool

ngOnInit() {
    subscription =  this.auth.isloggedin().subscribe((data)=>{
      this.bool = data;
    });
}

method() {
    if(this.bool) {
    your code...
    } else {
    your code...
    }
}
编辑3:

别忘了退订

编辑4:

对不起,我没有意识到这是一个http请求。但是概念是一样的,每次你想要检查用户是否登录时,你都需要更新bool(订阅isLoggedIn)。所以我认为这样做可以:

bool: boolean;

constructor() {}

ngOnInit() {
    this.checkUserIsLogged();
}

method() {
    if(this.bool) {
    your code...
    } else {
    your code...
    }
}

checkUserIsLogged(callback?){
    this.auth.isloggedin().subscribe((data)=>{
      this.bool = data;
    }, err => {}, () => {
        if (callback) {
            callback();
        }
    });
}

facadeMethod() {
    this.checkUserIsLogged(() => {
        this.method();     
  })
}

感谢您的回答,只是想知道:我们在nginit中订阅,但它只完成了一次,因为请求没有完成几次,所以this.bool将保持为false,我们不需要回忆订阅或类似的内容吗?谢谢洛;)在这种情况下我应该订阅吗?谢谢。guards订阅了返回的observable并使用该值。你从来没有订阅过守卫
bool

ngOnInit() {
    subscription =  this.auth.isloggedin().subscribe((data)=>{
      this.bool = data;
    });
}

method() {
    if(this.bool) {
    your code...
    } else {
    your code...
    }
}
bool: boolean;

constructor() {}

ngOnInit() {
    this.checkUserIsLogged();
}

method() {
    if(this.bool) {
    your code...
    } else {
    your code...
    }
}

checkUserIsLogged(callback?){
    this.auth.isloggedin().subscribe((data)=>{
      this.bool = data;
    }, err => {}, () => {
        if (callback) {
            callback();
        }
    });
}

facadeMethod() {
    this.checkUserIsLogged(() => {
        this.method();     
  })
}