使用 rxjs switchmap 编译 problem/error

compilation problem/error while using an rxjs switchmap

您好,我的代码出错了。我有一个 angular 5 formGroup,我正在尝试在里面使用管道操作和 switchMap。

但是它给我一个错误。以下是我的代码片段。

this.formGroup.valueChanges.pipe(
  switchMap(
      (formValue) => {
        console.log(formValue);
      }

  )

).subscribe();

我的错误如下

Argument of type '(formValue: any) => void' is not assignable to parameter of type '(value: any, index: number) => ObservableInput<{}>'.
  Type 'void' is not assignable to type 'ObservableInput<{}>'.ts(2345)

非常感谢任何帮助 谢谢

您必须 return observableswitchMap 中。 switchMap 切换到一个新的可观察对象,您可以根据先前的值有条件地进行。

如果你只想 console.log,我会使用 tap

this.formGroup.valueChanges.pipe(
  tap(
      (formValue) => {
        console.log(formValue);
      }

  ),

).subscribe();

======================编辑====================== ==== 我假设 this.generatePerformanceGraph(formValue); 是一个常规函数并且它不是 return 可观察的,那么你可以在 subscribe.

this.formGroup.valueChanges.pipe(
  tap( // you can remove this tap, it is just for logging
      (formValue) => {
        console.log(formValue);
      }

  ),

).subscribe(formValue => {
  this.generatePerformanceGraph(formValue);
});