rxjs管道:Observable类型的参数无法分配给参数

时间:2018-09-19 20:23:58

标签: angular rxjs6

我是rxjs和管道的新手-试图理解为什么我会收到此打字稿错误:“我发现Observable类型的参数不能分配给参数OperatorFunction”。有人可以向我解释一下吗?

目的是要求您输入“ Hello”,但是当通过管道传输数据时,将数据替换为“ Bye”。

  ngOnInit() {
    this.getHello()
      .pipe(this.getBye())
      .subscribe(data => console.log(data))
  }
  getHello() {
    return of("Hello")
  }
  getBye() {
    return of ("Bye")
  }
}

3 个答案:

答案 0 :(得分:1)

将地图用作管道运算符:

this.getHello()
  .pipe(map((data) => { return this.getBye() }))
  .subscribe(data => {
    console.log(data);
  });
getHello() {
  return of("Hello");
}
getBye() {
  return of("Bye");
}

详细检查可管道运算符的链接: https://angular.io/guide/rx-library https://blog.hackages.io/rxjs-5-5-piping-all-the-things-9d469d1b3f44

答案 1 :(得分:1)

管道方法接收一个OperatorFunction,但是您将其赋给getBye()方法,该方法返回一个Observable。您必须传递一个OperatorFunction,例如“ map”:

onclick

答案 2 :(得分:0)

创建自己的运算符时,必须将一个函数包装在一个函数中。当在流上设置新事件时,将调用内部函数。同时,您将可以访问流本身(sourceStream)。

有关工作示例,请参见Stackblitz。

 ngOnInit() {
      this.getHello()
          .pipe(this.getBye())
          .subscribe(data => console.log(data))
      }

      getHello() {
        return of("Hello")
      }

      getBye() {
        return (sourceSteam: any) => of("Bye")
      }
    }

https://stackblitz.com/edit/ng-stackoverflow-52413693?file=index.ts