forkJoin 无法在 Angular 6 中使用动态 URL 数组
forkJoin not working with dynamic array of URLs in Angular 6
import { Observable } from 'rxjs';
import 'rxjs/add/observable/forkJoin';
reqArray = [];
for (let i = 0; i < this.array.length; i++) {
this.reqArray.push(this.companyLocationService.locationsUpdate());
}
forkJoin(this.reqArray).subscribe(
data => {
console.log(data);
},
err => console.error(err)
);
当我在没有 for 循环的情况下以静态方式传递数据时,它工作正常。
forkJoin(
this.reqArray.push(this.companyLocationService.locationsUpdate(1)),
this.reqArray.push(this.companyLocationService.locationsUpdate(2)),
this.reqArray.push(this.companyLocationService.locationsUpdate(3))
).subscribe(
data => {
console.log(data);
});
但是,在我的例子中,我必须根据一些条件创建 URLs 数组,所以静态添加它是不可能的。
我该如何解决这个问题?
你能把下面一行代码放在一个函数中,然后在 this.reqArray.push(this.companyLocationService.locationsUpdate()) 行之后调用那个函数吗:-
forkJoin(this.reqArray).subscribe(
数据 => {
console.log(data);
},
err => console.error(err)
);
希望对您有所帮助。
import { Observable } from 'rxjs';
import 'rxjs/add/observable/forkJoin';
reqArray = [];
for (let i = 0; i < this.array.length; i++) {
this.reqArray.push(this.companyLocationService.locationsUpdate());
}
forkJoin(this.reqArray).subscribe(
data => {
console.log(data);
},
err => console.error(err)
);
当我在没有 for 循环的情况下以静态方式传递数据时,它工作正常。
forkJoin(
this.reqArray.push(this.companyLocationService.locationsUpdate(1)),
this.reqArray.push(this.companyLocationService.locationsUpdate(2)),
this.reqArray.push(this.companyLocationService.locationsUpdate(3))
).subscribe(
data => {
console.log(data);
});
但是,在我的例子中,我必须根据一些条件创建 URLs 数组,所以静态添加它是不可能的。 我该如何解决这个问题?
你能把下面一行代码放在一个函数中,然后在 this.reqArray.push(this.companyLocationService.locationsUpdate()) 行之后调用那个函数吗:-
forkJoin(this.reqArray).subscribe( 数据 => {
console.log(data);
},
err => console.error(err)
);
希望对您有所帮助。