簡體   English   中英

Angular HttpClient 需要 0 個類型參數,但得到 1 個

[英]Angular HttpClient Expected 0 type arguments, but got 1

我有一個服務,我向我的 API 執行 http 請求,然后我為其他目的提供服務,將 url 傳遞給 http 請求服務。 但是當我嘗試將模型添加到我的get請求時,我得到了Expected 0 type arguments, but got 1錯誤。

日歷.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');
    }

}

web-request.service.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}`);
  }


}

日歷-event.model.ts

export interface ICalendarEvent {
    id: number;
    start: string;
    end: string;
    title: string;
    estimate_number: string;
    brand: string;
    meters: string;
    floor: string;
    floor_type: string;
    plint_type: string;
    floor_installer: number;
    city: string;
    street: string;
    postal_code: string;
    province: string;
    notes: string;
}

如果要調用帶有類型參數的函數,則需要將泛型類型參數添加到聲明中。

如果您想獲得這樣做的全部好處,我建議向您的函數添加返回類型。

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM