property 'forkJoin' does not exist on type 'typeof observable' - angular2

被刻印的时光 ゝ 提交于 2019-11-30 07:03:59

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!