Alf*_*avo 3 pipe observable rxjs angular
我得到一个值,并根据返回值,如果数据在我第一次发送时实际返回并继续,否则如果没有返回,我将获得默认值并继续处理数据。
我的问题是在 IF 语句之后返回默认数据。我无法让它返回数据,而不是可观察/订阅
它看起来像这样:
getValuesFunction() {
const getFirstValues$ = this.ApiCall.getFirstValues();
this.subscription = getFirstValues$.pipe(
map(data => {
if (data.length === 0) {
// this line is the one I have a problem with
return this.processedStockApi.getDefaultValues().subscribe();
} else {
// this line returns fine
return data;
}
}),
switchMap(data => this.apiCall.doSomethingWithData(data))
).subscribe();
}
Run Code Online (Sandbox Code Playgroud)
// API调用
getDefaultValues() {
return this.http.get<any>(this.stockUrl + 'getSelectiveDeleteData');
}
Run Code Online (Sandbox Code Playgroud)
而不是map使用其与 Observables 一起工作的变体之一,例如concatMapor mergeMap(switchMap在这种情况下也能工作):
getFirstValues$.pipe(
concatMap(data => {
if (data.length === 0) {
// this line is the one I have a problem with
return this.processedStockApi.getDefaultValues();
} else {
// this line returns fine
return of(data);
}
}),
switchMap(data => this.apiCall.doSomethingWithData(data)),
).subscribe(...);
Run Code Online (Sandbox Code Playgroud)
请注意,这两个if-else块现在都返回 Observables。它是concatMap订阅他们并进一步发出他们的结果的人。
| 归档时间: |
|
| 查看次数: |
10375 次 |
| 最近记录: |