gpt4 book ai didi

angular - 如何正确返回 void Observable?

转载 作者:太空狗 更新时间:2023-10-29 16:56:38 25 4
gpt4 key购买 nike

在我的 Angular 4 应用程序中,我有一个必须返回 Observable 的方法.这个方法有3个条件。第一个和第二个条件进行 get 调用,但第三个条件什么都不做,在这种情况下我必须返回 Observable同样,因为此方法是 .flatmap 的第一部分运算符(operator)。所以要链接 .flatmap 的第二部分接线员,我需要一个 Observable从第一部分开始。

我试过了 return new Observable<void>(); ,但我有一个错误:

ERROR TypeError: Cannot read property 'subscribe' of undefined

这是调用服务加载数据的初始方法。

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 of(); 不创建流。

鉴于

返回(void 0);

会。

如果您正在模拟并且需要返回一个 void 流。就像我的生活一样。 [悲伤的 Pandas ]

关于angular - 如何正确返回 void Observable?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47121192/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com