Angular提供了一个有用的类,名为HttpClient
。它的方法通常带有一个称为options
的参数。这是来自client.d.ts
的示例:
/**
* Construct a GET request which interprets the body as an `ArrayBuffer` and returns the full event stream.
*
* @return an `Observable` of all `HttpEvent`s for the request, with a body type of `ArrayBuffer`.
*/
get(url: string, options: {
headers?: HttpHeaders | {
[header: string]: string | string[];
};
observe: 'events';
params?: HttpParams | {
[param: string]: string | string[];
};
reportProgress?: boolean;
responseType: 'arraybuffer';
withCredentials?: boolean;
}): Observable<HttpEvent<ArrayBuffer>>;
为什么options
参数不使用接口?如果要包装此方法,在将options
参数传递给此方法时,是否有很好的方法来获得类型安全?
示例:
getUrl(url: string, options: >>>what-goes-here<<<): Observable<any> {
return this.httpclient.get(url, options);
}
显然是一个简单的示例,但是我们也使用类似的方法来应用错误处理程序。