41

I was looking to experiment with a forkJoin mainly using the accepted answer here:

Angular2 Observable.forkJoin of observable variables - ReferenceError: Observable is not defined

I'm getting the above error message as forkJoin isn't available.

Anyone know why?

Community
  • 1
  • 1
thegunner
  • 6,883
  • 30
  • 94
  • 143

6 Answers6

55

Angular 6 changes this up a bit. forkJoin has been converted to a regular function so, instead of:

import {Observable} from 'rxjs/Observable';
...
return Observable.forkJoin(
    this.http.get('someurl'),
    this.http.get('someotherurl'));

Use:

import {forkJoin} from 'rxjs';
...
return forkJoin(
    this.http.get('someurl'),
    this.http.get('someotherurl'));

You can go to https://www.metaltoad.com/blog/angular-6-upgrading-api-calls-rxjs-6 for more explanation.

Narsters
  • 596
  • 5
  • 4
49

Did you do this?

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

You have to add methods individually.

Dan Cancro
  • 1,401
  • 5
  • 24
  • 53
  • 4
    Note that this is a static method, so as Dan wrote, you have to `import 'rxjs/add/observable/forkJoin'`, and _not_ `import 'rxjs/add/operator/forkJoin'`. Have been bitten by that twice... – Cito Oct 10 '17 at 16:33
13

You just need to replace

import { Observable } from 'rxjs/Observable';

by

import { Observable } from 'rxjs/Rx';

and it will start working

Warning: Do not use this method as it will include the entire RxJs library (increasing kb bundle size)

cracker
  • 4,900
  • 3
  • 23
  • 41
3

Check your version of Rxjs, if you are using Rxjs 6, use import { Observable } from 'rxjs/Observable'; import 'rxjs/add/observable/forkJoin'; won't work, use import { Observable, forkJoin } from 'rxjs' instead

Yue Yin
  • 166
  • 4
1

I have been running Angular version 5.2.0 and faced similar problem. So instead of calling observable with the following way

import { Observable } from 'rxjs/observable'

I imported Observable by including the entire RxJs library

import { Observable } from 'rxjs/Rx'

Then for the forkJoin, included the below import module

import 'rxjs/add/observable/forkJoin';

and code compiled successfully.

Deepak Tekchandani
  • 488
  • 2
  • 5
  • 15
1

For me, Angular 4 it helps:

import {Observable} from 'rxjs/Rx';
instead of:
import {Observable} from 'rxjs/Observable';
Carnaru Valentin
  • 1,690
  • 17
  • 27