'HttpRequest<FormData>' 类型的参数不可分配给 'string | Request' 类型的参数
Argument of type 'HttpRequest<FormData>' is not assignable to parameter of type 'string | Request'
我正在尝试 post 使用 http
客户端的文件
const req = new HttpRequest('POST', this.API, formData, { reportProgress: true });
this.http.request(req).subscribe(event => {
if (event.type === HttpEventType.UploadProgress) {...}
但是打字稿显示警告
Argument of type 'HttpRequest<FormData>' is not assignable to parameter of type 'string | Request'.
Type 'HttpRequest<FormData>' is not assignable to type 'Request'.
Property 'contentType' is missing in type 'HttpRequest<FormData>'.
执行后,我在控制台
中看到以下内容error
Error: First argument must be a url string or Request instance.
而根据 angular 的 http.request
定义,第一个参数可以是 string
或类型 Request
(method) Http.request(url: string | Request, options?: RequestOptionsArgs): Observable<Response>
另外我不明白 属性 contentType
缺失警告,这是否意味着我必须明确地将其包含在 header 中?
我这里有什么地方做错了吗?
编辑:我正在使用
"@angular/common": "6.0.0",
"@angular/http": "6.0.0",
从错误消息来看,您似乎正在使用 Http
instead of HttpClient
。尝试将 Http
更改为 HttpClient
并检查是否适合您。
不要忘记将 HttpClientModule
添加到 @NgModule
的 imports
数组中。
Http
的request
方法用于接受string | Request
类型的请求。另一方面,HttpClient
的 request
方法接受类型为 HttpRequest
的请求
我正在尝试 post 使用 http
客户端的文件
const req = new HttpRequest('POST', this.API, formData, { reportProgress: true });
this.http.request(req).subscribe(event => {
if (event.type === HttpEventType.UploadProgress) {...}
但是打字稿显示警告
Argument of type 'HttpRequest<FormData>' is not assignable to parameter of type 'string | Request'.
Type 'HttpRequest<FormData>' is not assignable to type 'Request'.
Property 'contentType' is missing in type 'HttpRequest<FormData>'.
执行后,我在控制台
中看到以下内容error
Error: First argument must be a url string or Request instance.
而根据 angular 的 http.request
定义,第一个参数可以是 string
或类型 Request
(method) Http.request(url: string | Request, options?: RequestOptionsArgs): Observable<Response>
另外我不明白 属性 contentType
缺失警告,这是否意味着我必须明确地将其包含在 header 中?
我这里有什么地方做错了吗?
编辑:我正在使用
"@angular/common": "6.0.0",
"@angular/http": "6.0.0",
从错误消息来看,您似乎正在使用 Http
instead of HttpClient
。尝试将 Http
更改为 HttpClient
并检查是否适合您。
不要忘记将 HttpClientModule
添加到 @NgModule
的 imports
数组中。
Http
的request
方法用于接受string | Request
类型的请求。另一方面,HttpClient
的 request
方法接受类型为 HttpRequest