Search code examples
angularrxjs6angular-observable

Why I get mergeMapTo is not a function when using rxjs?


I recently upgrade app from Angular 5 to Angular 8 and I change the imports rxjs to match the new version of rxjs which is rxjs 6 now.

I have the following import statement:

import { map, mergeMap, startWith, mergeMapTo } from 'rxjs/operators';

An I'm using it inside the component:

public myObservable: any;

 myMethod() {

    this.myObservable = timer(1, 1000);
        this.myObservable
        .mergeMapTo(this.myService.loadData(this.myId))
        .subscribe((data) => {
                this.myData = data;                
            }, (errRes)=>{
            console.log(errRes);
        });
  }

Solution

  • You will need to use the new RXJS syntax, by wrapping those operators in the pipe()
    like so:

    public myObservable: any;
    
         myMethod() {
    
            this.myTimer = timer(1, 1000);
                this.myObservable
                .pipe(mergeMapTo(this.myService.loadData(this.myId)))
                .subscribe((data) => {
                        this.myData = data;                
                    }, (errRes)=>{
                    console.log(errRes);
                });
          }
    

    See this article for more details:
    https://www.academind.com/learn/javascript/rxjs-6-what-changed/