Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Error rxjs_Observable__.Observable.forkJoin is not a function?

I am using Rxjs in an angualr-cli application.

in viewer.component.ts

    //Other Imports
    import { Observable } from 'rxjs/Observable';
    //omitting for brevity
   export class ViewerComponent implements OnInit, AfterViewInit, OnDestroy {
    someFunction(someArg){
    //omitting for brevity
    let someArray: any = [];
    //Add some info
    Observable.forkJoin(someArray).subscribe(data => {
              //Do something with data
            });
    }
    //omitting for brevity
    }  

I get and error as

ERROR TypeError: __WEBPACK_IMPORTED_MODULE_2_rxjs_Observable__.Observable.forkJoin is not a function
    at ViewerComponent.webpackJsonp../src/app/component/viewer.component.ts.ViewerComponent.someFunction(http://localhost:4200/main.bundle.js:4022:73)
    at http://localhost:4200/main.bundle.js:3951:31

But if I Import Rxjs completely (import 'rxjs';) everything works. No error. I cont seem to understand whats additional that needed. I also tries importing rxjs/Observable/forkjoin but nothing to avail.

Any pointers on how to go about this.

like image 892
Ankesh Avatar asked Jun 25 '17 06:06

Ankesh


People also ask

How do I use observable forkJoin?

The RxJS forkJoin() operator is a join operator that accepts an Array of ObservableInput or a dictionary Object of ObservableInput and returns an Observableand then waits for the Observables to complete and then combine last values they emitted.

How do I use forkJoin in angular 8?

There are a few steps to accomplish this tutorial:Install Angular CLI and Create Angular 8 Application. Create an Angular 8 Service. Display Multiple Details of Data using Angular 8 Material. Run and Test the Complete Angular 8 RxJS Example.

Is forkJoin deprecated?

ForkJoin method signature has changedThis is now deprecated and you must pass an array of observables to forkJoin operator.

Does forkJoin complete?

As forkJoin only completes when all inner observables complete, we must be mindful if an observable never completes. If this does happen forkJoin will never complete either.


2 Answers

As explained here, you have two options:

  1. Either import all operators as a single package
  2. Or import each operator individually

In the first case you would use import like this:

import Rx from 'rxjs/Rx';

Rx.Observable.forkJoin(1,2,3)

In the second like this:

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

I believe what you're looking for is the second option.

like image 131
Max Koretskyi Avatar answered Oct 13 '22 18:10

Max Koretskyi


For RxJS v6.5+, it is now:

import { forkJoin } from 'rxjs';

https://www.learnrxjs.io/operators/combination/forkjoin.html

like image 41
VSO Avatar answered Oct 13 '22 16:10

VSO