[英]Using if and else with combineLatest in Angular
我有一个包含 combineLatest 的 return 语句,它在内部调用两个函数。 我想为这些函数添加 if 和 else 块,但到目前为止我还没有这样做。
return combineLatest([
this.firstfunction();
this.secondfunction()
]).pipe(
map(_ => Success()),
catchError(error => {
return error
})
);
我想要 if(.... this.firstfunction() 和另一个 if().. this.secondfunction().. 目前,当我在 combineLatest 中添加 if 块时,它会出错。 有什么建议?
那这个呢:
const observables = [];
if(conditionForFirstFuction) {
observables.push(this.firstFunction());
}
if(conditionForSecondFunction) {
observables.push(this.secondFunction());
}
return combineLatest(observables).pipe(
map(_ => Success()),
catchError(error => {
return error
})
);
很难看到您想要什么,但您可以使用扩展运算符尝试此操作。 请注意,如果您最终得到一个空数组,当两个条件都为假时, combineLatest
将永远不会触发:
return combineLatest([
...( firstCondition ? this.firstfunction() : [] ),
...( secondCondition ? this.secondfunction() : [] )
]).pipe(
map(_ => Success())
);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.