如何订阅 flatMap 的结果?

How to subscribe on result of flatMap?

如何订阅flatMap的结果?

   timer(0, 2000)
      .pipe(
        flatMap(() => this.scannerService.scan(this.scanType)),
        takeWhile(res => res.errorDesc !== "SUCCESS")
      )
      .subscribe((res: IScanResponse) => {
});

我尝试在 pipe 之后使用 .subscribe()。但我收到错误消息:

You provided an invalid object where a stream was expected. You can provide an Observable, Promise, Array, or Iterable.

我的远程函数是:

public scan(scanType: string): Observable<any> {}

我的导入是:

import { timer } from "rxjs";
import { takeWhile } from "rxjs/operators";
import { ScannerService } from "src/app/api/ScannerService";
import { flatMap } from "rxjs/operators";

.pipe() 之后 .pipe() returns 一个可观察的

const tm = timer(0, 2000).pipe(
     flatMap(() => this.scannerService.scan(this.scanType)),
     takeWhile(res => res)
).subscribe(res=> {
//your code
});;

你的问题是你的类型 return编辑者 scan

flatMap中,你需要return一个Observable

在这里你 return Observable<any> {} 而不是 Observable<any>