ProgramingTip

rxjs 플랫 맵 누락

bestdevel 2020. 12. 26. 15:57
반응형

rxjs 플랫 맵 누락


여러 rx.js Observable을 연결하고 데이터를 전달합니다. Flatmap피팅 연산자 택하지 만

import { Observable } from 'rxjs/Observable';

없습니다.

Error TS2339: Property 'flatmap' does not exist on type 'Observable<Coordinates>'

5.0.0-beta.6rx.js 버전 이 사용됩니다.

public getCurrentLocationAddress():Observable<String> {
    return Observable.fromPromise(Geolocation.getCurrentPosition())
      .map(location => location.coords)
      .flatmap(coordinates => {
        console.log(coordinates);
        return this.http.request(this.geoCodingServer + "/json?latlng=" + coordinates.latitude + "," + coordinates.longitude)
          .map((res: Response) => {
                       let data = res.json();
                       return data.results[0].formatted_address;
              });
      });
  }

대답은 아주 간단합니다.

연산자는 mergeMap이 버전의 rxjs에서 호출 됩니다.

편집하다 :

또한 import 'rxjs/add/operator/mergeMap'.


제 경우에는 mergeMap에 대한 기능 보강을 가져와야했습니다.

import 'rxjs/add/operator/mergeMap';

flatMap은 mergeMap의 순서대로 위의 모듈을 가져옵니다.


RxJS 5.5 이상에서는 flatMap연산자의 이름이 mergeMap. 대신, mergeMap와 함께 연산자를 사용합니다 pipe.

일반적으로 flatMap을 사용하여 계속 사용할 수 있습니다 FlatMap.

RxJS v5.5.2는 Angular 5의 기본 버전입니다.

이제 가져 오는 가져 오는 각 RxJS 연산자 에 대해 mergeMap'rxjs / operator'에서 가져 오는 파이프 연산자를 사용합니다.

Http 요청 Observable에서 mergeMap을 사용하는 예

import { Observable } from 'rxjs/Observable';
import { catchError } from 'rxjs/operators';
...

export class ExampleClass {
  constructor(private http: HttpClient) {
    this.http.get('/api/words').pipe(
      mergeMap(word => Observable.of(word.join(' '))
    );
  }
  ...
}

여기에 공지 flatMap로 대체 mergeMap하고, pipe연산자는 도트 체인과 함께 사용하던 방식으로 운영을 구성하는 데 사용됩니다.


자세한 정보는 허용 연산자에 대한 rxjs 문서를 참조하십시오. https://github.com/ReactiveX/rxjs/blob/master/doc/lettable-operators.md


올바른 가져 오기는 다음과 달라집니다.

import { Observable } from 'rxjs/Observable';
import 'rxjs/add/operator/mergeMap';

모듈 mergeMap가져 오면 오면 flatMap코드에서 사용할 수 있습니다.

코드에서 import { Observable } from 'rxjs/Rx';추가 mergeMap가져 오기가 필요하지 않지만 AoT 보안 오류가 있습니다.

ERROR in ./node_modules/rxjs/_esm5/observable/BoundCallbackObservable.js
Module build failed: TypeError: Cannot read property 'type' of undefined

빠른 업데이트 -2019 년 5 월

rxjs v6.5.1 사용

mergeMap연산자 로 가져 오기 , 예 :

import { Observable, from, of } from "rxjs";
import { map, filter, mergeMap } from "rxjs/operators";

그런 다음 새로운 pipe기능 과 함께 사용하십시오.

var requestStream = of("https://api.github.com/users");
var responseStream = requestStream.pipe(
  mergeMap(requestUrl => {
    console.log(requestUrl);
    ... // other logic
    return rp(options);  // returns promise
  })
);

그것은 나를 위해 일했습니다!

import { Observable } from 'rxjs/Rx';

참조 URL : https://stackoverflow.com/questions/38481764/rxjs-flatmap-missing

반응형