Angular HttpClient期望0类型参数,但得到1

时间:2020-03-31 07:25:04

标签: angular typescript api rxjs observable

我有一个服务,在该服务中我向API执行http请求,然后为其他目的提供了将URL传递给http请求服务的服务。但是,当我尝试向我的 public IAdapterPresentation TryEndAuthentication(IAuthenticationContext context, IProofData proofData, System.Net.HttpListenerRequest request, out System.Security.Claims.Claim[] claims) { claims = null; IAdapterPresentation result = null; string pin = proofData.Properties["pin"].ToString(); if (pin == "12345") { System.Security.Claims.Claim claim = new System.Security.Claims.Claim("https://schemas.microsoft.com/ws/2008/06/identity/claims/authenticationmethod", "https://schemas.microsoft.com/ws/2012/12/authmethod/otp"); claims = new System.Security.Claims.Claim[] { claim }; } else { result = new AdapterPresentation("Authentication failed.", false); } return result; } 请求中添加模型时,出现get错误。

calendar.service.ts

Expected 0 type arguments, but got 1

web-request.service.ts

import { Injectable } from '@angular/core';
import { Observable } from 'rxjs';
// WebReq for http requests 
import { WebRequestService } from './web-request.service';
import { ICalendarEvent } from '../../models/calendar-event.model';


@Injectable({
    providedIn: 'root',
})

export class CalendarService {

    constructor(private webReqService: WebRequestService) { }

    // Where I get the error
    getCalendarEvent(): Observable<ICalendarEvent[]> {
        return this.webReqService.get<ICalendarEvent[]>('/calendars');
    }

}

calendar-event.model.ts

import { Injectable } from '@angular/core';
import { HttpClient } from '@angular/common/http';

@Injectable({
  providedIn: 'root'
})
export class WebRequestService {

  readonly _url: string;

  constructor(private http: HttpClient) {
    this._url = 'some url';
  }

  get(uri: string) {
    return this.http.get(`${this._url}/${uri}`);
  }

  post(uri: string, payload: Object) {
    return this.http.post(`${this._url}/${uri}`, payload);
  }

  patch(uri: string, payload: Object) {
    return this.http.patch(`${this._url}/${uri}`, payload);
  }

  delete(uri: string) {
    return this.http.delete(`${this._url}/${uri}`);
  }


}

1 个答案:

答案 0 :(得分:6)

如果要使用类型参数调用函数,则需要在声明中添加通用类型参数。

如果您想获得这样做的全部好处,我建议向您的函数添加一个返回类型。

get<T>(uri: string): Observable<T> {
  return this.http.get<T>(`${this._url}/${uri}`);
}