问题
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?
回答1:
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.
回答2:
Did you do this?
import { Observable } from 'rxjs/Observable';
import 'rxjs/add/observable/forkJoin';
You have to add methods individually.
回答3:
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)
回答4:
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
回答5:
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.
回答6:
For me, Angular 4 it helps:
import {Observable} from 'rxjs/Rx';
instead of:
import {Observable} from 'rxjs/Observable';
来源:https://stackoverflow.com/questions/38443798/property-forkjoin-does-not-exist-on-type-typeof-observable-angular2