我需要/希望在 forkJoin 中使用六个以上的参数。目前,根据对另一个相关问题的回答,似乎不可能向 forkJoin 发送超过 6 个参数。
然而,根据官方文档,它说“forkJoin 是一个运算符,它接受任意数量的 Observable,可以作为数组或直接作为参数传递。”
forkJoin - 官方文档
好吧,我正在这样做,我得到一个错误 TS2322:Type 'foo' is notassignable to type 'bar[]'。
在我的研究中,我还发现如果你有返回不同类型的承诺,最好不要将参数作为数组发送,因为这会将它们类型转换为所有相同的类型。-来源
这是我的代码。我正在使用最新版本的 Typescript 和 Angular 4。
ngOnInit() {
this.spinner.show();
Observable.forkJoin(
this.loadParams(), // Returns an Observable<Object>
this.service.getPromiseType1(), // The rest return Observable<Array>
this.service.getPromiseType2(),
this.service.getPromiseType3(),
this.service.getPromiseType4(),
this.service.getPromiseType5(),
this.service.getPromiseType6(),
).finally(() => this.spinner.hide())
.subscribe(
([param, promise1, promise2, promise3, promise4, promise5, promise6]) => {
this.job = job;
this.value1 = promise1;
this.value2 = promise2;
this.value3 = promise3;
this.value4 = promise4;
this.value5 = promise5;
this.value6 = promise6;
}, (error) => {errorHandlingFunction}
});
如果我删除任何单个参数以便将六个参数传递给 forkJoin,则它可以正常工作。所以我的问题是,在我想在一次调用中加载对象可观察对象和后续数组可观察对象的情况下,是否有另一种方法可以做到这一点?这是 forkJoin 的一个错误,因为官方文档说它应该能够接受任意数量的 Observables?
我尝试创建一个 Observable 类型的数组并在 forkJoin 中使用 array.forEach() 但它抱怨返回类型为 void。无论如何,这似乎是一种笨拙的方式。