通常,我使用角httpclient执行get请求,如下例所示:
this.http
.get<Project>('/api/projects/' + id)
.subscribe(p => {
// p is typed as Project
this.project = p;
});
类型参数
Project
定义预期的响应。
HttpCLient.get<T>()
返回
Observable<T>
是的。
我现在需要为请求添加一些选项,例如:
getRequestOptions() {
var headers = new HttpHeaders();
headers.append('Authorization', 'Basic ' + this.auth.EMail + ':' + this.auth.Token);
return { headers: this.getRequestOptions() };
}
// Somewhere else
this.http.get<Project>(url, this.getRequestOptions())
.subscribe(p => {
// p is HttpEvent<Project> with no (direct?) access to the
// returned object, just a property called type
});
这调用了
get()
方法,由于某些原因,它不返回
可观察的
但是一个
Observable<HttpEvent<T>>
是的。除了在我看来是个糟糕的设计之外:
如何使用httpevent?它只包含一个名为
type
.我知道在get请求的情况下,可能有派生类型
HttpResponse<T>
.我真的需要处理所有类型转换和转换的事情吗,仅仅因为我使用了requestoptions?
这个设计决定的背景是什么?为什么所有重载都返回同一类型并共享一个公共使用模式?