forkJoin is deprecated: resultSelector is deprecated, pipe to map instead

半腔热情 提交于 2019-12-30 05:33:05

问题


I'm working on an Angular 6 project.

Running ng lint gives the following Warning:

"forkJoin is deprecated: resultSelector is deprecated, pipe to map instead"

 forkJoin(...observables).subscribe(

Any idea? Can't seem to find any information about this deprecation.

I just generated a brand new Angular application "ng new forkApp" with Angular CLI: 6.1.5

source:

import { Component, OnInit } from '@angular/core';
import { HttpClient } from '@angular/common/http';
import { forkJoin } from 'rxjs';

@Component({
  selector: 'app-root',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.css']
})
export class AppComponent implements OnInit {
  title = 'forkApp';

  constructor(private http: HttpClient) {}

  ngOnInit() {
    console.log('ngOnInit...');

    const obs = [];
    for (let i = 1; i < 4; i++) {

      const ob = this.http.get('https://swapi.co/api/people/' + i);
      obs.push(ob);

    }

    forkJoin(...obs)
      .subscribe(
        datas => {
          console.log('received data', datas);
        }
      );

  }
}

"dependencies" section from package.json file:

  "dependencies": {
    "@angular/animations": "^6.1.0",
    "@angular/common": "^6.1.0",
    "@angular/compiler": "^6.1.0",
    "@angular/core": "^6.1.0",
    "@angular/forms": "^6.1.0",
    "@angular/http": "^6.1.0",
    "@angular/platform-browser": "^6.1.0",
    "@angular/platform-browser-dynamic": "^6.1.0",
    "@angular/router": "^6.1.0",
    "core-js": "^2.5.4",
    "rxjs": "^6.0.0",
    "zone.js": "~0.8.26"
  },

Once all three gets are done I got all data in "datas" array. The issue is that once I run: ng lint I got this:

C:\forkApp>ng lint

WARNING: C:/forkApp/src/app/app.component.ts[26, 5]: forkJoin is deprecated: resultSelector is deprecated, pipe to map instead


回答1:


I was able to fix this by getting rid of the ellipsis:

forkJoin(observables).subscribe();

As long as observables is already an array, it should have the same result.




回答2:


forkJoin should work. Which rxjs version are you using? Latest version should be doing this:

import { of, combineLatest, forkJoin } from 'rxjs';
import { map, mergeAll } from 'rxjs/operators';

Here the working code:

import { of, forkJoin } from 'rxjs';

const observables = [of('hi'), of('im an'), of('observable')];

const joint = forkJoin(observables);

joint.subscribe(
  s => console.log(s) 
)

Should output:

["hi", "im an", "observable"]

I tried reproducing this but I see no warning:

https://stackblitz.com/edit/angular-v4nq3h?file=src%2Fapp%2Fapp.component.ts




回答3:


accroding to forkJoin.d.js , the forkJoin(...args) is deprecated, you can write the other way. for your source code, since you already have an array with same type, just pass your array will be OK.

//forkJoin.d.js
import { Observable } from '../Observable';
import { ObservableInput } from '../types';
export declare function forkJoin<T>(sources: [ObservableInput<T>]): Observable<T[]>;
export declare function forkJoin<T, T2>(sources: [ObservableInput<T>, ObservableInput<T2>]): Observable<[T, T2]>;
export declare function forkJoin<T, T2, T3>(sources: [ObservableInput<T>, ObservableInput<T2>, ObservableInput<T3>]): Observable<[T, T2, T3]>;
export declare function forkJoin<T, T2, T3, T4>(sources: [ObservableInput<T>, ObservableInput<T2>, ObservableInput<T3>, ObservableInput<T4>]): Observable<[T, T2, T3, T4]>;
export declare function forkJoin<T, T2, T3, T4, T5>(sources: [ObservableInput<T>, ObservableInput<T2>, ObservableInput<T3>, ObservableInput<T4>, ObservableInput<T5>]): Observable<[T, T2, T3, T4, T5]>;
export declare function forkJoin<T, T2, T3, T4, T5, T6>(sources: [ObservableInput<T>, ObservableInput<T2>, ObservableInput<T3>, ObservableInput<T4>, ObservableInput<T5>, ObservableInput<T6>]): Observable<[T, T2, T3, T4, T5, T6]>;
export declare function forkJoin<T>(sources: Array<ObservableInput<T>>): Observable<T[]>;
export declare function forkJoin<T>(v1: ObservableInput<T>): Observable<T[]>;
export declare function forkJoin<T, T2>(v1: ObservableInput<T>, v2: ObservableInput<T2>): Observable<[T, T2]>;
export declare function forkJoin<T, T2, T3>(v1: ObservableInput<T>, v2: ObservableInput<T2>, v3: ObservableInput<T3>): Observable<[T, T2, T3]>;
export declare function forkJoin<T, T2, T3, T4>(v1: ObservableInput<T>, v2: ObservableInput<T2>, v3: ObservableInput<T3>, v4: ObservableInput<T4>): Observable<[T, T2, T3, T4]>;
export declare function forkJoin<T, T2, T3, T4, T5>(v1: ObservableInput<T>, v2: ObservableInput<T2>, v3: ObservableInput<T3>, v4: ObservableInput<T4>, v5: ObservableInput<T5>): Observable<[T, T2, T3, T4, T5]>;
export declare function forkJoin<T, T2, T3, T4, T5, T6>(v1: ObservableInput<T>, v2: ObservableInput<T2>, v3: ObservableInput<T3>, v4: ObservableInput<T4>, v5: ObservableInput<T5>, v6: ObservableInput<T6>): Observable<[T, T2, T3, T4, T5, T6]>;
/** @deprecated resultSelector is deprecated, pipe to map instead */
export declare function forkJoin(...args: Array<ObservableInput<any> | Function>): Observable<any>;
export declare function forkJoin<T>(...sources: ObservableInput<T>[]): Observable<T[]>;


来源:https://stackoverflow.com/questions/52486786/forkjoin-is-deprecated-resultselector-is-deprecated-pipe-to-map-instead

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