Angular中‘Observable<string>’无法赋值给目标Observable类型的解决方法
问题原因与修复方案
核心问题
你的getUser方法声明返回Observable<{ userId: string; partyId: string }>,但添加的concatMap操作符会将上游流的输出(用户数据对象)替换为persistPartyUserClaims$返回的Observable<string>,导致返回类型不匹配,触发编译错误。
修复代码
调整concatMap的逻辑,在完成存储操作后返回原始的用户数据对象,保证流的输出类型与方法声明一致:
public getUser(): Observable<{ userId: string; partyId: string }> { return this.userRepository.getLoggedInUser().pipe( map((user: User) => { const partyId = user.getUniqueClaimValue(this.claimTypesService.partyId); this.persistPartyUserClaims(partyId); return { userId: user.Id, partyId: partyId }; }), catchError((err) => throwError(() => err)), concatMap((userData) => this.persistPartyUserClaims$(userData.partyId).pipe( map(() => userData) ) ) ); }
修复说明
concatMap会等待persistPartyUserClaims$的存储操作完成,确保数据持久化后再继续流的传递- 内部通过
map(() => userData)将原始的用户数据对象重新注入流中,保证最终输出类型符合方法声明的要求
内容的提问来源于stack exchange,提问作者Fazla Elahi Md Jubayer
相关产品推荐
相关产品推荐

