我对一些行为主题有疑问。
我有一项服务:
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 始终未定义,即使它在我的服务中设置为真,我不明白为什么,因为另一个是真的......
我记录了所有我能找到的东西,但一无所获,如果你有任何想法,欢迎你:)
谢谢