RequestOptions 的 Angular HttpClient 替换? [英] Angular HttpClient replacement for RequestOptions?
问题描述
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.
附言我在这里阅读了答案,但它不使用类型:Angular4:Http ->HttpClient - requestOptions
P.S. I read the answer here, but it doesn't use a type: Angular4: Http -> HttpClient - requestOptions
推荐答案
现在,我只是在我的应用程序中名为 request-options 的新文件中定义了我自己的"
.接口定义如下,属性直接从 Angular 库的 RequestOptions
接口.tsclient.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)
我不确定我是否完全遗漏了某些东西,或者我应该针对 angular repo 进行 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.
这篇关于RequestOptions 的 Angular HttpClient 替换?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!