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/
Component Html Goes like this
<input type="text" placeholder="input first" id="input1">
<input type="text" id="input2" placeholder="input second">
<span></span>
import required functions
import { fromEvent } from 'rxjs'
import { map, mergeMap } from 'rxjs/operators'
var span = document.querySelector('span');
var input1 = document.querySelector('#input1');
var input2 = document.querySelector('#input2');
var obs1 = fromEvent(input1, 'input');
var obs2 = fromEvent(input2, 'input');
var obs1 = fromEvent(input1, 'input');
var obs2 = fromEvent(input2, 'input');
obs1.pipe(mergeMap(event1 => obs2.pipe(
map(event2 => (<HTMLInputElement>event1.target).value
+ " "
+ (<HTMLInputElement>event2.target).value))))
.subscribe((result) => {
span.textContent=result;
})