angular - 了解 Rxjs 可观察对象
问题描述
我对 RXJS 还很陌生
我很感激帮助理解为什么下面的代码返回一个Observable<Observable<any[]>>
而不是Observable<any[]>
?
getResults() {
// first endpoint to get the logged-in contact's id
return this.getContact()
.pipe(
switchMap((contact: any) => {
// second endpoint to get a contact's subordinates
return this.getSubordinates(contact[0].EntityId)
.pipe(
map((subs: any[]) => {
// if contact has subs, it means he or she are a supervisor
if (contacts.length) {
// get details of subs
var obsArr = contacts.map(c => {
// third endpoint to get a contact's details
return this.getDetailsOfContact(c.EntityId).pipe(
map(details => ({ details , c }))
);
})
// also get details of supervisor itself
var supervisorObs = this.getDetailsOfContact(contact.EntityId).pipe(
map(details=> (
{
details,
c: {
contactid: contact.Attributes.contactid,
fullname: contact.Attributes.fullname
}
}
)
)
)
obsArr.push(supervisorObs)
return obsArr;
} else {
// if contact does not have any subordinates
// it means they are not a supervisor
// then get details for supervisor only
var obsArr1: Observable<any>[] = [];
obsArr1.push(this.getDetailsOfContact(contact.EntityId).pipe(
map(details => (
{
details,
c: {
contactid: contact.Attributes.contactid,
fullname: contact.Attributes.fullname
}
}
)
)
));
return obsArr1;
}
}
)
);
})
)
}
最终,我希望getResults()
返回一个 Observable,它发出一个联系人数组。单个联系人(如果他们不是主管或如果他们是主管但没有任何下属,则为登录的联系人)或多个联系人(如果他们是主管并且有下属)
解决方案
this.getContact() 是一个 observable,getSubordinates() 也是一个 observable。getSubordinates() 是一个内部可观察对象。所以输出是正确的。
推荐阅读
- mvvm - 如何在 xamarin 表单的 ViewModel 类中使用 Navigation.InsertPageBefore?
- python - 芹菜“失败”的附加元数据
- xamarin - Xamarin Forms: MasterDetail button on a second page
- c++ - 如何只为非常量对象定义一些构造函数?
- python - 如何更新数据框行比较来自另一个数据框的数据
- jms - 如何安全地停止消息侦听器
- java - 我无法在 wildfly 8.2.0 服务器上部署此应用程序。我已经更改了 .jar 文件,但仍然弹出此错误
- django - 停止在 django appconfig ready() 函数中启动的服务?
- php - 解析/解码响应(Json 格式?) - PHP Curl
- python - 如何在 Telepot Telegram 机器人中加粗文本?