10

我正在尝试使用接口从 API 获取数据。Bellow是我的临时界面

export interface ITemp {
    id: number,
    name: string,
    age:  number
}

下面是我的 HTTP 服务,其中有一个 fn getHomedetails,它调用了一个 API。

import {Injectable} from '@angular/core';
import { HttpClient } from '@angular/common/http';
import { ITemp } from "../interfaces/temp";
import { Observable } from "rxjs/Observable";
import 'rxjs/Rx';

@Injectable()
export class HttpService{

    http:any;
    baseUrl: String;

    constructor(http:HttpClient){
        this.http = http;
        this.baseUrl = 'some_url';
    }

    getHomeDetails(): Observable<ITemp>  {
        return this.http.get<ITemp>(this.baseUrl); //problem is here 
        //when mouse is pointed on get<ITemp> it shows "Untyped function calls may not accept type arguments"

    }

}

未定义接口。我不知道我做错了什么。上面的语法是 Angular 4.3X 语法。我使用的编辑器是 sublime 和 visual studio。

4

1 回答 1

23

这是因为您正在为您的班级级别http提供以下类型any

更改http:any;http: HttpClient

一个好的经验法则是不要使用any,除非你真的必须这样做。

于 2018-01-12T14:57:57.143 回答