AngularFireAuth.user 当放置在 withLatestFrom RxJS 运算符中时,observable 不会发出
AngularFireAuth.user observable won't emit when placed in withLatestFrom RxJS operator
如果 AngularFireAuth.user
observable 是源 observable,则它工作正常,例如
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()
我做错了什么,我该如何解决?我需要当前的身份验证状态,但源可观察对象需要与那个不同的可观察对象。
那是因为用户流还没有发出任何东西。我相信解决此问题的最简单方法是为用户流添加 startsWith(null)
withLatestFrom(this.AngularFireAuth.user.pipe(startsWith(null)))
observable AngularFireAuth.user
在 withLatestfrom()
的使用过程中可能没有发射。
您可以使用 combineLatest
运算符。当其中一个传递的 observable 发出时,运算符发出最新值。在您的情况下,这是 AngularFireAuth.user
可观察到的。
of("test")
.pipe(
tap(console.log), // log showing up
combineLatest(this.AngularFireAuth.user),
tap(console.log) // log not showing up
).subscribe()
Combines multiple Observables to create an Observable whose values are
calculated from the latest values of each of its input Observables.
如果 AngularFireAuth.user
observable 是源 observable,则它工作正常,例如
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()
我做错了什么,我该如何解决?我需要当前的身份验证状态,但源可观察对象需要与那个不同的可观察对象。
那是因为用户流还没有发出任何东西。我相信解决此问题的最简单方法是为用户流添加 startsWith(null)
withLatestFrom(this.AngularFireAuth.user.pipe(startsWith(null)))
observable AngularFireAuth.user
在 withLatestfrom()
的使用过程中可能没有发射。
您可以使用 combineLatest
运算符。当其中一个传递的 observable 发出时,运算符发出最新值。在您的情况下,这是 AngularFireAuth.user
可观察到的。
of("test")
.pipe(
tap(console.log), // log showing up
combineLatest(this.AngularFireAuth.user),
tap(console.log) // log not showing up
).subscribe()
Combines multiple Observables to create an Observable whose values are calculated from the latest values of each of its input Observables.