RxJs主题错误-无法读取未定义的属性“订阅" [英] RxJs Subject error - cannot read property 'subscribe' of undefined
本文介绍了RxJs主题错误-无法读取未定义的属性“订阅"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
在返回Observable
的服务方法中,我试图通过Subject
通知组件操作已完成.
In my service method that returns Observable
I'm trying to notify the component via Subject
that an action completed.
completed: Subject<boolean>
constructor(private http: Http) {
}
loadItems(): Observable<FrontItemDto[]> {
return this.http.get ( `${ServiceSettings.ApiUrl}/front` )
.map ( res => {
res.json ();
if ( res.json () ) {
this.completed.next ( true );
}
} )
.catch ( (error: any) => Observable.throw ( error.json ().error || 'Server error' ) );
}
这是组件监听Subject
的方式:
ngOnInit(): void {
this.getItems();
this.sub = this.dataService.completed.subscribe(completed => {
if (completed) {
this.show = false;
}
});
}
但是我收到一条错误消息,指出未定义主题(已完成).我在做什么错了?
But I'm getting an error saying that Subject (completed) is undefined. What am I doing wrong?
推荐答案
您尚未正确初始化completed
主题
completed = new Subject<boolean>(); //it should have parenthesis at the end
演示 (@Ompurdy)
Demo by @Ompurdy
这篇关于RxJs主题错误-无法读取未定义的属性“订阅"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文