Angular 类型';()=>;无效';不可分配给类型';(移动方向:任意)=>;布尔值';

Angular 类型';()=>;无效';不可分配给类型';(移动方向:任意)=>;布尔值';,angular,Angular,我一直收到以下错误消息: Type '() => void' is not assignable to type '(MovingDirection: any) => boolean' 我所要做的就是将订阅的结果用作布尔值,而不是可观察值 canEnterStep2仅接受布尔值 我需要从canEnterStep2内部调用它,然后根据输出返回布尔值true或false 但它似乎不起作用,我被困在想法中 有解决办法吗 canEnterStep2: (MovingDirection)

我一直收到以下错误消息:

Type '() => void' is not assignable to type '(MovingDirection: any) => boolean'
我所要做的就是将订阅的结果用作布尔值,而不是可观察值

canEnterStep2仅接受布尔值

我需要从canEnterStep2内部调用它,然后根据输出返回布尔值true或false

但它似乎不起作用,我被困在想法中

有解决办法吗

  canEnterStep2: (MovingDirection) => boolean = () => {
    this.checkUsernameExists().subscribe(
      res => {
          if (res) {
              // do some stuff here
              return true
          } else {
              // do some stuff here
              return false
          }
      })
   }
checkUsernameExists():可观察{
返回this.userService.getUserByUsername(this.username.toLowerCase().trim()).pipe(映射(结果=>{
如果(结果){
如果((result.length>0)&&(result[0]['username']==this.username.toLowerCase().trim())&&(result[0]['uid']!==this.user.uid)){
this.logger.debug('用户名属于另一个用户');
返回true;
}否则{
this.logger.debug('用户名不属于其他用户');
返回false;
}
}否则{
this.logger.debug('无法确定用户名');
返回错误
}
}));
}

如果要异步检查某些条件,则必须始终异步。差不多

canEnterStep2: (MovingDirection) => Observable <boolean> = () => {
   if (/* ...conditions... */) {
      return this.setUserPersonalDetails();
   } else {
      // display and/or log error messages here, then:
      return of(false);
   }
}

setUserPersonalDetails(): Observable <boolean> {
   // is ngxLoader an asynchronous function? 
   // if yes, make it the start of the observable chain, and switchMap()
   this.ngxLoader.start(); 

   return this.userService.getUserByUsername(this.username.toLowerCase().trim())
      .pipe(
         tap( /* do side effects here */ ),
         map( /* check if result satisfies conditions, return true or false accordingly */ )
      );
};
canEnterStep2:(移动方向)=>Observable=()=>{
如果(/*…条件…*/){
返回此.setUserPersonalDetails();
}否则{
//在此处显示和/或记录错误消息,然后:
归还(假);
}
}
setUserPersonalDetails():可观察{
//ngxLoader是一个异步函数吗?
//如果是,则将其作为可观察链的起点,并切换到switchMap()
这个.ngxLoader.start();
返回this.userService.getUserByUsername(this.username.toLowerCase().trim())
.烟斗(
轻触(/*此处做副作用*/),
map(/*检查结果是否满足条件,相应地返回true或false*/)
);
};

canEnterStep2
的使用者也必须将其视为异步流。

您能说明您在哪里使用
canEnterStep2
方法吗?不幸的是,这不起作用。。。canEnterStep2必须是布尔值
canEnterStep2: (MovingDirection) => Observable <boolean> = () => {
   if (/* ...conditions... */) {
      return this.setUserPersonalDetails();
   } else {
      // display and/or log error messages here, then:
      return of(false);
   }
}

setUserPersonalDetails(): Observable <boolean> {
   // is ngxLoader an asynchronous function? 
   // if yes, make it the start of the observable chain, and switchMap()
   this.ngxLoader.start(); 

   return this.userService.getUserByUsername(this.username.toLowerCase().trim())
      .pipe(
         tap( /* do side effects here */ ),
         map( /* check if result satisfies conditions, return true or false accordingly */ )
      );
};