首页 > 解决方案 > “typeof Observable”类型上不存在属性“forkJoin”。角 8

问题描述

我正在尝试使用方法 forkJoin 执行多个 http 请求。我需要像一个简单的请求一样返回所有值,但我遇到了一些错误。

我使用的是角度版本 8.2.3,我的 rxjs 版本是 ~6.3.3

查看我的服务代码:

import { Injectable } from '@angular/core';
import { BehaviorSubject } from 'rxjs';
import { HttpClient, HttpHeaders } from '@angular/common/http';
import { map, catchError } from 'rxjs/operators';
import 'rxjs/add/observable/forkJoin';
import { Observable } from 'rxjs';

const PRAGMA = 'pragma';
const NO_CACHE = 'no-cache';
const CACHE_CONTROL = 'Cache-Control';
const URL = 'https://pokeapi.co/api/v2/';

@Injectable({
    providedIn: 'root'
})

export class PokemonsService {

    public loadPokemonByQuantity$: BehaviorSubject<any> = new BehaviorSubject(undefined);

    constructor(private http: HttpClient) { }

public loadPokemonByQuantity(initial: number, final: number):Observable<any>  {
        const callArray = [];
        const headers = new HttpHeaders({ CACHE_CONTROL: NO_CACHE, PRAGMA: NO_CACHE });
        for (let index = initial; index <= final; index++) {
            callArray.push(this.http.get<any>(`https://pokeapi.co/api/v2/pokemon/${index}`, { headers }));
        }
        return Observable.forkJoin(callArray)
    }
}

堆栈闪电战

标签: angularangular8

解决方案


使用“rxjs”:“^6.5.2”,像这样使用它:

import { forkJoin } from 'rxjs';
forkJoin(...callArray).subscribe(() => {
       // Do something
    });

推荐阅读