AngularFireAuth.user observable在与LatestFrom RxJS运算符放置时不会发出



如果AngularFireAuth.user是源可观察对象,则可以正常工作,例如

this.AngularFireAuth.user.subscribe((u) => console.log(u))

但是阻塞我的可观察流,如果我把它放在withLatestFrom操作符,例如

of("test")
.pipe(
tap(console.log), // log showing up
withLatestFrom(this.AngularFireAuth.user),
tap(console.log)  // log not showing up
).subscribe()

我做错了什么,我该如何解决?我需要当前的auth状态,但源可观察对象必须是另一个可观察对象

这是因为用户流还没有发出任何东西。我认为解决这个问题最简单的方法是为用户流

添加startsWith(null)
withLatestFrom(this.AngularFireAuth.user.pipe(startsWith(null)))

观测到的AngularFireAuth.user可能在withLatestfrom()的使用过程中没有释放。您可以使用combineLatest操作符。当传递的可观察对象之一发出时,操作符发出最新的值。在你的例子中,这是AngularFireAuth.user可观察对象。

of("test")
.pipe(
tap(console.log), // log showing up
combineLatest(this.AngularFireAuth.user),
tap(console.log)  // log not showing up
).subscribe()

combineLatest文档

组合多个Observable来创建一个Observable,它的值为根据每个可观察对象的最新值计算。

最新更新