首页 > 解决方案 > 为什么我在使用 rxjs 时得到 mergeMapTo 不是函数?

问题描述

我最近将应用程序从 Angular 5 升级到 Angular 8,并更改了导入 rxjs 以匹配新版本的 rxjs,即 rxjs 6。

我有以下导入语句:

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

我在组件内部使用它:

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);
        });
  }

标签: angularrxjs6angular-observable

解决方案


您将需要使用新的 RXJS 语法,将这些运算符包装成pipe()
如下所示:

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);
            });
      }

有关更多详细信息,请参阅本文:
https ://www.academind.com/learn/javascript/rxjs-6-what-c​​hanged/


推荐阅读