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?
Try structuring this way your service:
Service:
Remember to add
MyService
to providers.Where you update your client (if this is the case), you do something like this:
Component (The one that triggers):
Component (The one that is Subscribed):
From what I understood, you are trying to pass data from 1 component to another. You get your Client's data and send it over your App to another component, right? That's why I posted this solution.
If you are interested in other types of subscriptions, read this:
Angular 2 special Observables (Subject / Behaviour subject / ReplaySubject)
The purpose of
BehaviorSubject
is to provide initial value. It can benull
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 withnext
.It likely should be