rxjs - Angular 5 - 订阅行为主题时的问题
问题描述
在一项服务的构造函数中订阅行为主题变量时,我遇到了问题。当ok 的值为 0但我正在做时,订阅第一次发生。next(1)在服务 1中,订阅未在服务 2 的构造函数中发生。
Service 1 : Where behavior subject variable is emitting
counter: number = -1;
ok: BehaviorSubject<any> = new BehaviorSubject(0);
if (this.counter === 0) {
setTimeout(() => {
this.checkCounterFinally();
}, 1000);
}
}
checkCounterFinally() {
if (this.counter === 0) {
this.ok.next(1);
}
}
Service 2: Where i am subscribing the behavior subject variable in constructor.
constructor(private service: Service1){
this.service.ok.subscribe((val) => {
console.log("service2", val);
if (val=== 1) {
this.getDataFromLocalStorage();
}
});
}
解决方案
BehaviorSubject 的使用看起来不错。您的问题的根源可能是您的应用程序中有许多 Service1 实例。
推荐阅读
- azure - 如何使用 Get-AzVM 输出订阅名称
- kotlin - Kotlin 协程与 webflux 的优势
- salesforce - 解决 Storybooks 与 Salesforce Platform 集成的自定义标签
- r - 将标签添加到使用 plot_geo 创建的绘图地图
- django - 如何在管理站点中自定义多对多内联
- postgresql - Postgres - 在 Do Block 中更改角色(匿名代码块)
- python - 单击链接时,Selenium 退出全屏
- tensorflow - TensorFlow 中的通道选择
- javascript - 无法通过附加到 Edge 浏览器在 VS Code 中进行调试
- linux - 在 Centos 中调整窗口大小