Issue
private customer: Subject<Object> = new BehaviorSubject<Object>(null);
setCustomer(id, accountClassCode) {
this.customer.next({'id': id, 'accountClassCode': accountClassCode});
}
getCustomer() {
return this.customer.asObservable();
}
I'm using this part of code but I'm getting an error that can not find id of null. Is there any solution to get initial value that is not null?
Solution
The purpose of BehaviorSubject
is to provide initial value. It can be null
or anything else. If no valid initial value can be provided (when user id isn't known yet), it shouldn't be used.
ReplaySubject(1)
provides a similar behaviour (emits last value on subscription) but doesn't have initial value until it is set with next
.
It likely should be
private customer: Subject<Object> = new ReplaySubject<Object>(1);
Answered By - Estus Flask
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.