获取 BehaviorSubject 的值 [英] Get the value of a BehaviorSubject
问题描述
我正在尝试获取 BehaviorSubject 的值.返回值,但我如何利用它们在 return true/false 语句中使用它们.
I am attempting to get the values of a BehaviorSubject. The values are returned but how to i utilized them to use them in a return true/false statement.
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:"name@gmail.com"
1:"Bob Smith"
2:{adminUser: false, basicUser: true} length:3
__proto__:Array(0)
__proto__:Subject
我的行为主体已向其发送以下内容.为什么我一开始就为空.
My Behavior Subject has the following being sent to it. Why am i getting null at the start.
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,因此有一个错误的响应.然后它再次运行并变为真.这一切都发生在最初运行页面之后.我需要我的检查值在加载时为真.
I run console.log and the value shows up null first therefore has a false response. Then it runs again and becomes true. This all happens after running the page initially. I need for my check value to be true on load.
推荐答案
您应该能够使用 .getValue() 调用 BehaviorSubject 的名称所以像:
You should just be able to call the name of the BehaviorSubject with the .getValue() so something like:
let x = new BehaviorSubject<any>;
...
x.getValue()
这篇关于获取 BehaviorSubject 的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!