在代码中使用switchMap而不是withLatestFrom操作符。在AngularFireAuth对象中,建议使用switchMap操作符来获取用户observables。下面是一个示例代码:
import { AngularFireAuth } from '@angular/fire/auth';
import { switchMap } from 'rxjs/operators';
...
constructor(private afAuth: AngularFireAuth) {}
...
this.afAuth.user.pipe(
switchMap(user => {
if (user) {
// do something with the user data
} else {
// handle the case where the user is not logged in
}
})
).subscribe();
使用switchMap操作符,我们可以订阅Firebase的用户observables,并在该用户observables发出新值时执行某些操作。因此,我们不再需要使用withLatestFrom操作符来获取用户observables的值。