问题描述
Angular的Http
模块曾经具有一个RequestOptions
对象,该对象可以传递给其get
方法(例如,this.http.get(fooUrl, barOptions)
.RequestOptions
包含任何标头.这是已弃用.已弃用的RequestOptions
的最近替代品"是 HttpRequest .
Angular's Http
module used to have a RequestOptions
object which could be passed to its get
method (e.g. this.http.get(fooUrl, barOptions)
. The RequestOptions
contained any headers. This was deprecated. The "closest replacement" for the deprecated RequestOptions
is HttpRequest.
这有两个问题:
1. HttpRequest
有一个url
参数,因此它不等同于RequestOptions
.实际上,它似乎封装了整个请求,但是我看不到它在任何地方使用,因此我猜测它只是一个内部类. 2. HttpClient
的get
方法不将HttpRequest
作为参数.
There's two problems with this:
1. HttpRequest
has a url
param, so it is NOT an equivalent of RequestOptions
. In fact, it seems to encapsulate the entire request, yet I don't see it used anywhere, so I am guessing it's just an internal class..
2. HttpClient
's get
method does NOT take HttpRequest
as an argument.
HttpClient
的get
方法的源代码如下:
The source code for HttpClient
's get
method looks like this:
/**
* Constructs a `GET` request that interprets the body as an `ArrayBuffer` and returns the response in
* an `ArrayBuffer`.
*
* @param url The endpoint URL.
* @param options The HTTP options to send with the request.
*
* @return An `Observable` of the response, with the response body as an `ArrayBuffer`.
*/
get(url: string, options: {
headers?: HttpHeaders | {
[header: string]: string | string[];
};
observe?: 'body';
params?: HttpParams | {
[param: string]: string | string[];
};
reportProgress?: boolean;
responseType: 'arraybuffer';
withCredentials?: boolean;
}): Observable<ArrayBuffer>;
现在可以在options
参数中使用某些类型吗?我应该只定义一个上面带有 HttpHeaders 的接口吗?如果没有它的类型,为什么不呢?
Is s there some type I can use for the options
param now? Should I just define an interface that has HttpHeaders on it? If there isn't a type for it, why not?
我的旧代码如下:
reqOptions.headers.set('Authorization', user.token);
reqOptions.headers.set('RefreshToken', user.refreshToken);
旧版Http
模块中的
RequestOptions
具有headers属性,并且是静态键入的.
RequestOptions
from the old Http
module had a headers property and was statically typed.
P.S.我在这里阅读了答案,但没有使用类型: Angular4:Http-> HttpClient-requestOptions
P.S. I read the answer here, but it doesn't use a type:Angular4: Http -> HttpClient - requestOptions
推荐答案
目前,我只是在应用程序request-options.ts
的新文件中定义了我自己的" RequestOptions
接口.接口定义如下,这些属性直接从Angular Library的client.d.ts
文件中提出,以匹配HttpClient
从options
参数中进行的REST调用期望:
For now, I just defined "my own" RequestOptions
interface in a new file in my app called request-options.ts
. The interface definition is below, the properties were lifted straight out of the Angular Library's client.d.ts
file to match what HttpClient
expects from the options
param for REST calls:
import { HttpHeaders, HttpParams } from '@angular/common/http';
export interface RequestOptions {
headers?: HttpHeaders;
params?: HttpParams;
}
现在,我可以再次静态键入RequestOptions
,例如:
Now I can statically type RequestOptions
again, e.g.:
const requestOptions: RequestOptions = {
params: new HttpParams()
};
(以上示例代码是从此处提取的: Angular4:Http-> HttpClient-requestOptions )
(Above example code lifted from here: Angular4: Http -> HttpClient - requestOptions)
我不确定我是否完全遗漏了一些东西,还是应该针对有角度的仓库进行PR.如果没有理由,这似乎是一个相当明显的遗漏.
I am not sure if I am completely missing something or I should go make a PR against the angular repo. This seems like a rather obvious omission if there isn't a reason for it.
这篇关于角HttpClient替换RequestOptions?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!