取消订阅 switchmap 中的第一个可观察对象

Unsubscribe first observable in switchmap

我有两个 observable,第二个需要第一个的结果,所以我用了 switchMap :

this.fooSharer.getFoo().pipe(switchMap((foo: Foo) => {
    return this.http.post('/someUrl',
    body,
    headers
  )
}));

所以我在 return 中有一个 Observable of Object(来自 http.post),但是我对 foo Observale 的订阅仍然有效,我无法取消订阅,我该怎么做?

如果您使用 fooSharer.getFoo() 发出的值作为启动第二个 Observable 的信号,您可以将 take(1) 放在 switchMap 之前。 这样,在第一个发出值之后 fooSharer.getFoo() 将完成。