[英]Unable to access property of data through BehaviorSubject. Property does not exist on BehaviorSubject
While using BehaviourSubject to set shared data across my application, I am unable to access the properties of the data.使用 BehaviourSubject 在我的应用程序中设置共享数据时,我无法访问数据的属性。 Getting error :-
获取错误:-
** **
property does not exist on type 'BehaviourSubject'
'BehaviourSubject' 类型不存在属性
** **
SharedService.ts共享服务.ts
setCommonData(data): void {
this.commonData.next(data);
}
getCommonData(): Observable<BehaviorSubject<any>> {
return this.commonData;
}
app.component.ts app.component.ts
// result = {id: number, name: string, info: {}}
getData(): void {
this.dataService.getApi(url, {}, true).subscribe((result) => {
this.sharedService.setCommonData(result);
}
}
child.component.ts子组件.ts
displayData(): void {
this.sharedService.getCommonData().subscribe((data) => {
this.displatData = data // works
this.info = data.info // property info does not exist on type 'BehaviourSubject<any>'
});
}
You're returning an observable of the BehaviorSubject
.您正在返回
BehaviorSubject
的可观察对象。 Instead you need to return the BehaviorSubject
as an observable.相反,您需要将
BehaviorSubject
作为可观察对象返回。
Try the following尝试以下
setCommonData(data): void {
this.commonData.next(data);
}
getCommonData(): Observable<any> { // <-- return `Observable` here
return this.commonData.asObservable();
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.