angular - BehaviorSubject undefined in constructor -
i have problem behaviorsubjects.
i have service :
import{ injectable } '@angular/core'; import { behaviorsubject } 'rxjs/behaviorsubject'; @injectable() export class boutonimprimergriseservice { boutonimprimergrise = new behaviorsubject<boolean>(true); boutonimprimergrisechanged$ = this.boutonimprimergrise.asobservable(); boutonmettreazerogrise = new behaviorsubject<boolean>(true); boutonmettreazerogrisechanged$ = this.boutonmettreazerogrise.asobservable(); }
and use in component :
export class gestioncompteurscomponent { boutonimprimergrise: boolean; boutonmettreazerogrise: boolean; private _boutonimprimergrisesubscription: subscription; private _boutonmettreazerosubscription: subscription; constructor(private _boutonimprimergriseservice: boutonimprimergriseservice) { this._boutonimprimergrisesubscription = this._boutonimprimergriseservice.boutonimprimergrisechanged$.subscribe( value => { console.log(value); this.boutonimprimergrise = value; } ); this._boutonmettreazerosubscription = this._boutonimprimergriseservice.boutonmettreazerogrisechanged$.subscribe( value => { console.log(value); this.boutonmettreazerogrise = value; } ); } }
the problem when log values of observables in constructor, first one, boutonimprimergrise undefined if it's set true in service , don't understand why because other 1 true...
i logged found nothing, if have idea you're welcomed :)
thanks
a couple things:
i typically define variables within service private unless need them not reason, , other thing use typescript define boutonimprimergrise variable behaviorsubject. (repeat both declarations)
private boutonimprimergrise: behaviorsubject<boolean> = new behaviorsubject<boolean>(true); boutonimprimergrisechanged$ = this.boutonimprimergrise.asobservable();
Comments
Post a Comment