Angular 角度http post方法给出类型脚本错误

Angular 角度http post方法给出类型脚本错误,angular,typescript,typeerror,angular-http,Angular,Typescript,Typeerror,Angular Http,我用的是角5。i通过服务与API交互。我有一个方法,我想用它发布到API中。问题是,如果我将post方法的类型指定给“候选”对象,我会得到以下警告: src/app/candidates.service.ts(17,12): error TS2558: Expected 0 type arguments, but got 1. 我的函数代码如下所示: addCandidate(candidate: Candidate): Observable<Candidate> { retu

我用的是角5。i通过服务与API交互。我有一个方法,我想用它发布到API中。问题是,如果我将post方法的类型指定给“候选”对象,我会得到以下警告:

src/app/candidates.service.ts(17,12): error TS2558: Expected 0 type arguments, but got 1.
我的函数代码如下所示:

  addCandidate(candidate: Candidate): Observable<Candidate> {
return this.http.post<Candidate>(this.apiUrl, candidate, httpOptions);} 
addCandidate(候选者:候选者):可观察{
返回this.http.post(this.apirl,candidate,httpOptions);}

我认为我在这里没有正确使用类型,但我不确定。http post方法是否返回候选类型?如果不只是更改为
Observable
而不是
Observable

,我建议删除
,因为在post方法中,它只需要发送json数据。您还已经在
addCandidate(candidate:candidate)
方法参数声明中键入了它。

这可能是因为您使用的是旧的、不推荐使用的
Http
类,而不是
HttpClient

import {Http} from '@angular/http'; //old, deprecated client. Does not support generic types
import {HttpClient} from '@angular/common/http'; //new client
将服务构造函数中的
private http:http
替换为
private http:HttpClient
,然后像这样导入HttpClient

import {HttpClient} from '@angular/common/http`
别忘了在AppModule模块中导入HttpClientModule

//app.module.ts
import { HttpClientModule } from '@angular/common/http';

....
imports: [HttpClientModule]

谢谢你的回答。我从看得见的变成了看得见的。然而,错误依然存在。这似乎是由post方法之后,post函数参数之前指定的类型引起的。如果我从那里删除它,则会发生另一个错误(类型为“{headers:HttpHeaders;}”的参数不可分配给类型为“RequestOptionsArgs”的参数)。请尝试
Observable
。它类似于只有
可观察的
,只是它不应该抛出任何错误。但实际上,如果您希望POST请求返回数据,那么应该将该泛型的类型设置为POST响应数据的类型?httpClient方法签名依赖于多种内容http选项如下所示:const httpOptions={headers:new HttpHeaders({'Content-Type':'application/json'})};我基本上需要上传一些对象到api。谢谢你的回答。我也这么想,但我想保持一致。非常感谢。看起来我确实不得不在构造函数中更改http客户机。