【发布时间】:2023-03-11 16:35:01
【问题描述】:
我们有一个处理视图管理的组件和一个管理服务器交互和数据解析的服务。
服务 postForm 方法返回一个共享的 observable 给组件。 服务订阅它,组件订阅它。
成功后,服务回调方法会处理一些数据 同样在成功或错误时,组件回调会更新视图中的反馈。
问题:组件错误回调仅在服务订阅函数包含错误回调时触发。
我是否使用了错误的模式? 如果不是,为什么我在两个订阅函数中都需要错误回调来让组件之一工作?
谢谢
组件:
onSubmit(): void {
this.service.postForm().subscribe(
() => this.onSuccessfulPost(),
()=>this.onErrorPost()
);
}
服务:
postForm() {
//Code here assembles url and body variables
this.currentObservable = this.http.post(url, body)
.map((response: Response) => this.parseResponse(response))
.catch((err: Response) => this.onHttpError(err)).share();
this.currentObservable.subscribe(
(response: any) => this.onSuccessfulPost(response),
() => {return} //WITHOUT THIS LINE COMPONENT CALL FAILS
);
return this.currentObservable;
}
【问题讨论】:
标签: angular rxjs angular-httpclient