forkJoin不使用Angular 6中的动态URL和POST对象数组

问题描述 投票:0回答:1

这是我的技术堆栈

Angular - 6.1.2

TypeScript - 2.7.2

rxjs - 6.2.2

这是我在Angular中的forkJoin方法的代码。在后端调用发布请求。但是这些参数并没有通过。

  import { Observable } from 'rxjs';
  import 'rxjs/add/observable/forkJoin';

        reqArray = [];

        for (let i = 0; i < this.array.length; i++) {

            if(array[i]==true)
            {
                let obj = { name: 'Test', status: array[i] };

                this.reqArray.push(this.http.post(url, { params: obj }).pipe(map((res: Response) => res)));

            }

      }




forkJoin(this.reqArray).subscribe(
       data => {


            console.log(data);

       },
       err => console.error(err)
    );

尝试过这种方法,但我得到了同样的回应。

    Observable.forkJoin(this.reqArray).subscribe(
       data => {

             console.log(data);

       },
       err => console.error(err)
    );

当我以静态方式传递没有for循环的数据时,它工作正常。

    forkJoin(
        this.http.get(url, { params: obj }).pipe(map((res: Response) => res)),
        this.http.get(url, { params: obj2 }).pipe(map((res: Response) => res)),
        this.http.get(url, { params: obj3 }).pipe(map((res: Response) => res)),
        this.http.get(url, { params: obj4 }).pipe(map((res: Response) => res))

    ).subscribe(
        data => {

            console.log(data);

        });

但是,在我的情况下,我将不得不根据一些条件创建URL数组,因此静态添加它是不可能的。

我应该在代码中更改什么?

angular rxjs fork-join
1个回答
2
投票

你需要传播数组

forkJoin(...this.reqArray)
© www.soinside.com 2019 - 2024. All rights reserved.