我有一个简单的场景:
服务.ts:
private showComposeBoxAction = new BehaviorSubject<boolean>(null);
showComposeBox = this.showComposeBoxAction.asObservable();
openComposeBox(event:boolean) {
console.log("in openComposeBox");
this.showComposeBoxAction.next(event);
}
组件.ts:
constructor(
private _service: Service,
) {
this.subscriptions.add(
this._mailingService.showComposeBox.subscribe(event => {
if (event) {
this.displayCompose = true;
console.log("showComposeBox displayCompose", this.displayCompose);
}
})
);
}
组件2.ts:
showComposeBox() {
if (this.count === 0) {
this._service.openComposeBox(true);
}
}
我在 openComposeMsg() 中记录了一条消息。我面临的问题是,我第一次正确订阅 showComposeBox observable 但第二次订阅时即使没有调用下一个,因为 msg "in openComposeBox" 没有登录到控制台。
无法理解 BehaviorSubject 的行为。我究竟做错了什么?