Angular http post方法提供了打字稿错误

时间:2018-02-27 18:15:11

标签: angular typescript typeerror angular-http

我正在使用Angular 5.我通过服务与API进行交互。我有一个方法,我想用于发布到API。问题是,如果我将post方法的类型指定为“Candidate”对象,我会收到以下警告:

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);} 

我想我在这里没有正确使用类型,但我不确定。

3 个答案:

答案 0 :(得分:0)

http post方法是否返回候选者?如果不只是更改为<svg version="1.1" id="Izolovaný_režim" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" x="0px" y="0px" viewBox="0 0 294.7 320.3" xml:space="preserve"> <pattern id="img" patternUnits="userSpaceOnUse" width="100%" height="100%" x="50%" y="50%"> <image xlink:href="https://cdn-mf0.heartyhosting.com/sites/mensfitness.com/files/styles/wide_videos/public/1280-fit-man-work-desk.jpg?itok=xpQGNXMI" x /> </pattern> <g> <polygon points="147.4,320.3 294.7,235.2 294.7,85.1 147.4,0 0,85.1 0,235.2 " fill="url(#img)"/> </g> </svg>而不是Observable

答案 1 :(得分:0)

这可能是因为您使用旧的,已弃用的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]

答案 2 :(得分:-1)

我建议删除<Candidate>,因为在post方法中,它所关心的只是你发送了json数据。您还在addCandidate(candidate: Candidate)方法参数声明中输入了它。