【发布时间】:2018-04-17 16:28:07
【问题描述】:
在我的 Angular 4 应用程序中,我有一个必须返回 Observable 的方法。该方法有3个条件。第一个和第二个条件进行 get 调用,但第三个条件什么也不做,在这种情况下,我还必须返回一个 Observable,因为此方法是 .flatmap 运算符的第一部分。所以要链接.flatmap 运算符的第二部分,我需要第一部分的Observable。
我尝试过return new Observable<void>();,但出现错误:
错误类型错误:无法读取未定义的属性“订阅”
这是调用服务加载数据的初始方法。
loadModelData() {
this.customerService.getModelDetail(this.code)
.subscribe(response => {
this.selected = response.body as Customer;
});
}
}
这是必须链接 2 个调用的服务方法。
getModelDetail(code) {
const params = new HttpParams().set('projection', 'withBalance');
return this.endPointUrlService.loadMaps(this.entityLink)
.flatMap((res) => {
return this.http.get(this.endPointUrlService.cutLinks(
this.endPointUrlService.map.get('customers')) + '/' + code, {observe: 'response', params: params})
.map((response) => <any>response);
})
}
这是来自支持服务的方法。 checkIfMapIsReady() 是在第三种情况下返回void Observable 的方法:
loadMaps(entityLink: EntityLink[]): Observable<void> {
console.log(entityLink);
for (const i in entityLink) {
if (entityLink.hasOwnProperty(i)) {
return this.checkIfMapIsReady(entityLink[i].name, entityLink[i].searchMap, entityLink[i].endPoints[0])
}
}
}
public checkIfMapIsReady(modelName: string, mapName: string, endPoints: string) {
if (this.map.get(modelName) === undefined) {
console.log('endpoint url service All maps undefined ' + modelName);
return this.getLinks(modelName, this.mapNames.get(mapName), false)
} else {
console.log('endpoint url service Populate only ' + mapName);
if (this.mapNames.get(mapName).get(endPoints) === undefined) {
return this.getLinks(modelName, this.mapNames.get(mapName), true)
} else {
return new Observable<void>();
}
}
}
【问题讨论】:
-
这能回答你的问题吗? Return an empty Observable