【发布时间】:2017-07-22 21:01:50
【问题描述】:
在返回Observable 的服务方法中,我试图通过Subject 通知组件操作已完成。
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;
}
});
}
但我收到一条错误消息,提示主题(已完成)未定义。我做错了什么?
【问题讨论】:
标签: angular typescript rxjs subject-observer