Error rxjs_Observable__.Observable.forkJoin is not a function?

后端 未结 4 513
情深已故
情深已故 2021-02-05 10:25

I am using Rxjs in an angualr-cli application.

in viewer.component.ts

    //Other Imports
    import { Observable } from \'rxj         


        
相关标签:
4条回答
  • 2021-02-05 10:41

    Use the forkJoin Operator as a function by importing forkJoin instead of importing Observable.
    Use forkJoin.subscribe() in place of Observable.forkJoin.subscribe()

    import { forkJoin } from 'rxjs/observable/forkJoin'
    
    export class ViewerComponent implements OnInit, AfterViewInit, OnDestroy {
        someFunction(someArg){
          let someArray: any = [];
          forkJoin(someArray).subscribe(data => {
          });
        }
    }
    

    Source Code : https://github.com/ReactiveX/rxjs/blob/master/src/internal/observable/forkJoin.ts

    0 讨论(0)
  • 2021-02-05 10:43

    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.

    0 讨论(0)
  • 2021-02-05 10:56

    For RxJS v6.5+, it is now:

    import { forkJoin } from 'rxjs';

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

    0 讨论(0)
  • 2021-02-05 11:00

    In one project I used the following forkjoin code to upload images, it works perfectly.

    /**
           *  Upload multiple image and then update the feature
           */
          public uploadImages(apiParams: any[], imageItems: any[]): Observable<any> {
            if (imageItems.length === 0) return;
    
            let promises = [];
            for (let imageItem of imageItems) {
              promises.push(this.uploadImage(apiParams, imageItem));
            }
            return Observable.forkJoin(...promises);
          }
    
          /**
           * upload single image
           */
          public uploadImage(apiParams: any[], imageItem: any): any {
            let _formData = new FormData();
            _formData.append("UploadedImage", imageItem,  (new Guid()).toString()+'.png');
            const imageApiUrl = StringFormatter.format(imagePoints.create, apiParams);
            return Observable.fromPromise(
              fetch(imageApiUrl, {
                method: "POST",
                body: _formData
              })
              .then(result => result.json())
            );
          }
    
    0 讨论(0)
提交回复
热议问题