我遇到了一些行爲主體的問題。構造函數中未定義的行爲主題
我有一個服務:
import{ Injectable } from '@angular/core';
import { BehaviorSubject } from 'rxjs/BehaviorSubject';
@Injectable()
export class BoutonImprimerGriseService {
boutonImprimerGrise = new BehaviorSubject<boolean>(true);
boutonImprimerGriseChanged$ = this.boutonImprimerGrise.asObservable();
boutonMettreAZeroGrise = new BehaviorSubject<boolean>(true);
boutonMettreAZeroGriseChanged$ = this.boutonMettreAZeroGrise.asObservable();
}
我用它在一個組件:
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;
}
);
}
}
問題是,當我登錄的觀測值在構造函數中,第一個, boutonImprimerGrise總是未定義,即使它在我的服務中設置爲true,我也不明白爲什麼,因爲另一個是真的...
我記錄了所有我可以但沒有找到的東西,如果y OU有什麼想法,你是歡迎:)
感謝
謝謝你的建議。 –