【发布时间】:2019-04-15 15:31:52
【问题描述】:
我在我的应用程序中使用 Behavioursubject 我使用异步管道订阅了 Behavioursubject 并且我已经订阅了我的组件。
问题:当我尝试更新 Behavioursubject 中的数据时,数据会在 Behavioursubject 中更新。组件订阅未更新。在应用程序启动订阅执行时。在基于更改启动后,我正在更新服务中的数据,服务中的数据正在更新,但组件内的订阅没有更新。
我尝试了多种方法,例如将服务注册更改为根模块,但也没有奏效
这是我的组件代码:
这是调用的方法订阅。
ngOnInit() {
this.getnotifications();
}
getnotifications() {
this.notifications.getjsnotifications('all');
this.notifications.jsnotifications.subscribe(data => {
console.log("came after the update");
this.jsnotifications = data;
});
}
服务代码如下:
public jsnotifications = new BehaviorSubject(null);
constructor(){
this.getjsnotifications("all");
}
getjsnotifications(type){
this.http.get<Config>(`${environment.JS_API}/jobseeker/notifications/getJobSeekerNotifications?notificationType=`+type).subscribe(data =>{
console.log(data.map.notificationsVo);
this.jsnotifications.next(data.map.notificationsVo);
this.jsnotifications.subscribe(data => {
console.log(data);
})
this.testbehaviour.next(data.map.notificationsVo);
console.log("data updated");
});
}
请帮我解决问题。
提前致谢。
【问题讨论】:
-
我建议您重新表述您的问题。真的很难跟上。另外,我看不到您在组件中的何处使用该服务来发出 http 请求。 ngOnInit() 函数不使用您的服务。
-
请提供 stackblitz 链接,以便更好地帮助您
-
ok 有多个模块在这个复制问题很困难
标签: angular rxjs angular7 rxjs6