我不确定我是否能够正确解释这一点:
我正在使用angular4编写数据服务:
clang++ helloworld.cpp -target x86_64-win32
helloworld.cpp:1:10: fatal error: 'iostream' file not found
#include <iostream>
^
1 error generated.
这很好用。但现在,我需要从getProjects(): Observable<Project[]> {
return some_observable.map((rawProjects: RawProject[]) => {
return rawProjects.map((rawProject: RawProject) => {
// do some processing
return new Project(...);
});
});
}
获取一个ID,并从另一个返回可观察的API的API中获取数据:
RawProject
getProjects(): Observable<Project[]> {
return some_observable.map((rawProjects: RawProject[]) => {
return rawProjects.map((rawProject: RawProject) => {
const vendorID = rawProject.getVendorID();
getVendorInfo(vendorID) // returns Observable<VendorInfo>
return new Project(...);
});
});
}
会返回getVendorInfo(vendorID)
,而且我不确定如何返回需要包含Observable<VendorInfo>
的新Project
。是否可以在不更改方法签名的情况下包含VendorInfo
?
答案 0 :(得分:2)
如果您要等待所有可观察对象的解析,则需要使用switchMap
和forkJoin
。对于属性的“合并”,您需要使用Object.assign
:
getProjects(): Observable<Project[]> {
return some_observable.switchMap((rawProjects: RawProject[]) => {
let arrayOfProjectsObservables = rawProjects.map((rawProject: RawProject) => {
const vendorID = rawProject.getVendorID();
return getVendorInfo(venodorID) // returns Observable<VendorInfo>
.map((vendorInfo: VendorInfo) => {
//do some processing
let project = new Project(rawProject);
return Object.assign(project, vendorInfo);
})
});
return Observable.forkJoin(arrayOfProjectsObservables);
});
}