嵌套RXJS可观察值

时间:2020-05-12 20:59:58

标签: typescript rxjs observable rxjs6

我正在使用RXJS 6.5.4

在打字稿服务中,我有:

=MID(A1,B1+1,FIND(")",A1,B1)-B1-1)

第二种方法,我需要验证会话是否已通过身份验证,然后请求当前用户并将其转换为另一个Observable,以解决当前用户是否为管理员。

我正在尝试,但是出现错误:

=MID(A1,MIN(FIND({"(1","(2","(3","(4","(5","(6","(7","(8","(9","(0"},A1 & "(1(2(3(4(5(6(7(8(9(0"))+1,FIND(")",A1,MIN(FIND({"(1","(2","(3","(4","(5","(6","(7","(8","(9","(0"},A1 & "(1(2(3(4(5(6(7(8(9(0")))-MIN(FIND({"(1","(2","(3","(4","(5","(6","(7","(8","(9","(0"},A1 & "(1(2(3(4(5(6(7(8(9(0"))-1)

这是编译错误:

isAuthenticated(): Observable<boolean> {
   // request
}
currentUserPromise(): Promise<User> {
   // request the User model and return via Promise.
}

1 个答案:

答案 0 :(得分:2)

如果您需要订阅一个内部可观察的对象,则需要一个高阶运算符,例如switchMapmap用于同步数据转换。 mapTo是一个运算符,不能像您使用它时那样使用,请使用of将值转换为可观察的值...您还需要返回它才能订阅。 / p>

isAdmin(): Observable<boolean> {   
   return this.isAuthenticated().pipe(switchMap(authenticated => {
      if (authenticated) {   
          return from(this.currentUserPromise().then(user => user.roles.indexOf('ADMIN') > -1))
      }
      return of(false); // there is no authentication, so no current user and no admin!
   }));
}