Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/401.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
在路由器中使用订阅可以在angular2/4失败时激活_Angular_Typescript_Rxjs_Angular2 Services - Fatal编程技术网

在路由器中使用订阅可以在angular2/4失败时激活

在路由器中使用订阅可以在angular2/4失败时激活,angular,typescript,rxjs,angular2-services,Angular,Typescript,Rxjs,Angular2 Services,我试图保护我的路线,但我遇到了一个错误 Type 'Subscription' is not assignable to type 'boolean | Promise<boolean> | Observable<any>' 类型“Subscription”不能分配给类型“boolean” |承诺|可观察| 因此,在路线上,警卫有 canActivate(route: ActivatedRouteSnapshot, state: RouterState

我试图保护我的路线,但我遇到了一个错误

Type 'Subscription' is not assignable to type 'boolean 
     | Promise<boolean> | Observable<any>'
类型“Subscription”不能分配给类型“boolean”
|承诺|可观察|
因此,在路线上,警卫有

  canActivate(route: ActivatedRouteSnapshot, state: RouterStateSnapshot): Observable<any> | Promise<boolean> | boolean {
     return this._authService.checkLoggedin().subscribe((res)=>{
     if(!res){
        console.log("res is not here..")
      }
      return res;

    },(err)=>{

     console.log("error is", err);
  })
 }   
canActivate(路由:ActivatedRouteSnapshot,状态:RouterStateSnashot):可观察的|承诺|布尔值{
返回此。\u authService.checkLoggedin().subscribe((res)=>{
如果(!res){
console.log(“res不在这里…”)
}
返回res;
},(错误)=>{
log(“error is”,err);
})
}   
所以我的authservice我有

  checkLoggedin(): Observable<any> {
    if (!this._accesstokenService.getToken('access_token')) {
      return Observable.of(false); //just checks for localstorage
     }
      //if the access token exists  validate it on the server
    return this._http.post(this.authurl + 'auth/is-loggedin', 1)
      .map((res) => {
       return Observable.of(res);
      });
 }
checkLoggedin():可观察{
if(!this.\u accesstokenService.getToken('access\u token')){
返回Observable.of(false);//只检查本地存储
}
//如果访问令牌存在,请在服务器上验证它
返回此。\u http.post(this.authurl+'auth/is loggedin',1)
.map((res)=>{
可观察的回报率(单位:res);
});
}

我哪里做错了。我仍然想继续使用checkloggedin方法中的observable

您需要在您的
canActivate中返回observable而不是订阅

canActivate(route: ActivatedRouteSnapshot, state: RouterStateSnapshot): Observable<any> | Promise<boolean> | boolean {
     return this._authService.checkLoggedin();
 }  
canActivate(路由:ActivatedRouteSnapshot,状态:RouterStateSnashot):可观察的|承诺|布尔值{
返回此项。_authService.checkLoggedin();
}  

您的返回签名说明了这一点,但如果我是您,我会让它返回布尔值并保持简单。

您需要在
canActivate中返回一个可观察的而不是订阅

canActivate(route: ActivatedRouteSnapshot, state: RouterStateSnapshot): Observable<any> | Promise<boolean> | boolean {
     return this._authService.checkLoggedin();
 }  
canActivate(路由:ActivatedRouteSnapshot,状态:RouterStateSnashot):可观察的|承诺|布尔值{
返回此项。_authService.checkLoggedin();
}  

您的返回签名说明了这一点,但如果我是您,我会让它返回布尔值并保持简单。

但是返回承诺不会使它在服务器上验证访问令牌时保持同步。您从哪里获得承诺?observable不是PromiseServable就像promises一样工作看到了吗:它们用于异步活动我试图得到checkLoggedin的结果,它返回一个observable,在canactivate中我无法得到结果,在这种情况下,我不得不订阅checkLoggedin observable的结果,但是失败了是什么问题此外,可观察到的事物可以做的远不止承诺。所以我的问题是,为什么您的服务方法会返回一个承诺?但是返回一个承诺不会使它在服务器上验证访问令牌时保持同步,您从哪里获得承诺?observable不是PromiseServable就像promises一样工作看到了吗:它们用于异步活动我试图得到checkLoggedin的结果,它返回一个observable,在canactivate中我无法得到结果,在这种情况下,我不得不订阅checkLoggedin observable的结果,但是失败了是什么问题此外,可观察到的事物可以做的远不止承诺。所以我的问题是,为什么您的服务方法会返回承诺?