我正在尝试获取BehaviorSubject 的值。返回值,但我如何利用它们在 return true/false 语句中使用它们。
BehaviorSubject {_isScalar: false, observers: Array(0), closed: false, isStopped: false, hasError: false, …}
closed:false
hasError:false
isStopped:false
observers:[Subscriber]
thrownError:null
value:(...)
_isScalar:false
_value:Array(3)
0:"[email protected]"
1:"Bob Smith"
2:{adminUser: false, basicUser: true} length:3
__proto__:Array(0)
__proto__:Subject
我的行为主体收到以下内容。为什么我一开始就得到空值。
user: BehaviorSubject<User> = new BehaviorSubject<User>(null);
// 用户的行为角色。
this.afAuth.authState
.switchMap(auth => {
if (auth) {
/// signed in
const authData = this.db.list<User>('users/' + auth.uid).valueChanges();
console.log(authData);
return this.db.list<User>('users/' + auth.uid).valueChanges();
} else {
/// not signed in
return Observable.of(null);
}
})
// .subscribe(console.log); // check the subscription
.subscribe((userData) => {
console.log(userData);
this.user.next(userData);
// this.user.next(user);
// console.log(this.user.next(userData));
});
}
我遇到问题的结果是:
Check Value false authguard.service.ts:39
Route was False authguard.service.ts:26
Check Value true
我运行 console.log ,该值首先显示为 null,因此有错误响应。然后再次运行,结果变为 true。这一切都发生在最初运行页面之后。我需要我的检查值在加载时为真。