【发布时间】:2021-01-11 15:29:56
【问题描述】:
我正在尝试将数据从配置文件组件实时更新到标头组件,最初它可以正常工作,但在更新配置文件组件标头可观察的任何值后,不再订阅。所以我在这样的组件中使用带有 BehaviorSubject 和 Subscription 的服务。请检查我的代码,看看我的代码有什么问题:
服务代码:
//Service (code related to the problem):
this.userInfo = new BehaviorSubject(JSON.parse(localStorage.getItem('userData')));
this.updatedPageData = new BehaviorSubject('');
this.userValue = this.userInfo.asObservable();
配置文件组件
//updateData for emit updated value
updateData(data){
this.storage.saveDataInStorage(data);
this.cmnFunction.userInfo.next(data);
}
标题组件
//Get values
this.cmnFunction.userValue.subscribe(data=>{
if(data != null) {
this.currentUseInfo.name = data.name;
this.currentUseInfo.image = data.image;
this.currentUseInfo.role = data.role;
}
})
谁能告诉我我的代码有什么问题?
【问题讨论】:
-
你能做一个演示吗?很难说可能出了什么问题。你确定你只有一个服务实例吗?
-
看起来您的订阅应该会收到新的价值。我和@martin 一起做这个,你确定你只有一个服务实例吗?
-
@BizzyBob:是的,先生,我已经创建了一个服务实例,但我不明白这是什么问题?
-
@martin:好的,我将为此创建一个演示并创建一个服务实例
标签: angular rxjs behaviorsubject angular2-changedetection