I am trying to use mergeMap
in rxjs6
and i am getting this error:
Property 'mergeMap' does not exist on type 'Observable<{}>'
I have tried import 'rxjs/add/operator/mergeMap';
and it is not working.
What am i doing wrong?
import {from, Observable} from 'rxjs';
export class Test {
public doSomething(): Observable<any> {
return from(...).mergeMap();
}
}
That's correct, the "patch" style of operators has been removed since RxJS 6. You should better update your code to use only "pipeable" operators or install rxjs-compat
package that provides backwards compatibility with RxJS 5.
For more detailed description see official doc: https://github.com/ReactiveX/rxjs/blob/master/docs_app/content/guide/v6/migration.md
... more specifically this part: https://github.com/ReactiveX/rxjs/blob/master/docs_app/content/guide/v6/migration.md#backwards-compatibility
Thanks to the answer given by @martin, i was able to get it working with the new pipe
operations in rxjs6
. Here is my working code.
import {from, Observable} from 'rxjs';
import {mergeMap} from 'rxjs/operators';
export class Test {
public doSomething(): Observable<any> {
return from(...).pipe(mergeMap(...));
}
}
Import the individual operators, then use pipe instead of chaining.
import { map, filter, catchError, mergeMap } from 'rxjs/operators';
source.pipe(
map(x => x + x),
mergeMap(n => of(n + 1, n + 2).pipe(
filter(x => x % 1 == 0),
scan((acc, x) => acc + x, 0),
)),
catchError(err => of('error found')),
).subscribe(printResult);
Source: https://auth0.com/blog/whats-new-in-rxjs-6/
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With