Nice programing

rxjs 플랫 맵 누락

nicepro 2020. 12. 27. 20:44
반응형

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의 별칭이므로 위의 모듈을 가져 오면 flatMap을 사용할 수 있습니다.


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

별칭을 사용하여 flatMap을 계속 사용할 수 있습니다 FlatMap.

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

를 포함하여 가져 오는 각 RxJS 연산자에 대해 mergeMap이제 'rxjs / operators'에서 가져 와서 파이프 연산자를 사용해야합니다.

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 월

Using rxjs v6.5.1

Import as a mergeMap operator, eg/

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

Then use in conjunction with the new pipe feature, eg/

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

It worked for me!

import { Observable } from 'rxjs/Rx';

ReferenceURL : https://stackoverflow.com/questions/38481764/rxjs-flatmap-missing

반응형