【发布时间】:2021-05-09 07:51:28
【问题描述】:
我有一个 Angular 服务。该服务的方法调用 API 并返回一个对象。现在我想修改那个服务功能。我希望如果 API 调用返回对象,那么函数将对象作为可观察对象返回,但 API 返回 null,那么函数将返回一串消息。这是我的角度代码:
getAccountDetailsById(id: number): Observable<AccountInfo> | string {
return this.service.get<AccountResultInfo>(`${this.apiPATH}accountById/${id}`)
.pipe(map(user => {
if (user.Account) {
return user.Account;
} else {
return user.Message;
}
}));
}
但它显示以下错误:
键入'可观察的
' 不可分配给类型 “可观察”。 键入'字符串 | AccountInfo' 不可分配给类型 'AccountInfo'。 类型 'string' 不能分配给类型 'AccountInfo'。
【问题讨论】: