在我的服务方法中,返回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;
}
});
}
但我收到的错误是主题(已完成)未定义。我做错了什么?
答案 0 :(得分:5)
您尚未正确初始化completed
主题
completed = new Subject<boolean>(); //it should have parenthesis at the end