65
votes
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?

3
Did you try an empty object? new BehaviorSubject<Object>({}); - Bunyamin Coskuner
im gettin an error Supplied parameters do not match any signature of call target. - None
There's a solution. Do not use BehaviorSubject if you don't need initial value. - Estus Flask
I dont need inital value but my service is not working if i dont use BehaviorSubject - None
@windmaomao because if you then have any new subscriptions created afterwards they ever won't get that value - Simon_Weaver

3 Answers

136
votes

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);
5
votes

Try structuring this way your service:

Service:

@Injectable()
export class MyService {
    customerUpdate$: Observable<any>;

    private customerUpdateSubject = new Subject<any>();

    constructor() {
        this.customerUpdate$ = this.customerUpdateSubject.asObservable();
    }

    updatedCustomer(dataAsParams) {
        this.customerUpdateSubject.next(dataAsParams);
    }
}

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):

constructor(private myService: MyService) {
        // I'll put this here, it could go anywhere in the component actually
        // We make things happen, Client has been updated
        // We store client's data in an object
        this.updatedClient = this.myObjectWithUpdatedClientData;  // Obj or whatever you want to pass as a parameter
        this.myService.updatedCustomer(this.updatedClient);
    }

Component (The one that is Subscribed):

this.myService.customerUpdate$.subscribe((updatedClientData) => {
            // Wow! I received the updated client's data
            // Do stuff with this data!
        }
    );

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)

2
votes

I have found many cases where I want the simplicity of ReplaySubject(1) but also want the value property of BehaviorSubject, i.e. it stores your most recent value for retrieval without having to subscribe. And using BehaviorSubject was always annoying given the need for an initial value that gets broadcast, a condition I rarely wanted. To that end I created my own CurrentSubject.

import { ReplaySubject } from "rxjs";

export class CurrentSubject<T> extends ReplaySubject<T> {
    value: T;

    set(value?: T) {
        this.value = value;
        this.next(value);
    }
}

I wanted to override the next method but I couldn't get that to work for some reason, so I settled on a method called set. My override attempt looked like this...

export class CurrentSubject<T> extends ReplaySubject<T> {
    value: T;

    next(value?: T) {
        this.value = value;
        super.next(value);
    }
}

I don't know why overriding didn't work.