简体   繁体   English

Angular 4.3 HttpClient GET与类型

[英]Angular 4.3 HttpClient GET with Type

I have the following service: 我有以下服务:

@Injectable()
export class ExerciseService {

    constructor(private http:HttpClient) {}

    public getExercises(): Observable<Exercise> {
        return this.http.get<Exercise>('http://localhost:8090/exercise/1');
    }
}

interface Exercise {
    name: string;
}

What I get from this call is this: 我从这次电话中得到的是:

{ defaultValue: 10, id: 1, name "string" } {defaultValue:10,id:1,名称“字符串”}

But because I am passing a type to my call I expected to only get the name back from this function. 但是因为我在调用中传递了一个类型,所以我希望只能从该函数取回该名称。 I'm just getting started with Angular 4 and I don't see what's wrong :/ 我刚开始使用Angular 4,但看不出有什么问题:/

Types in TypeScript are static compile-time hints to the compiler (and possibly your IDE) to help you while writing code. TypeScript中的类型是编译器(可能还包括您的IDE)的静态编译时提示,以帮助您编写代码。 Your server cannot know about the specified type and will return the same data as always. 您的服务器无法知道指定的类型,因此将一如既往地返回相同的数据。 It doesn't even know it that code has been written in TypeScript when it was called. 甚至不知道代码是在调用时以TypeScript编写的。

Use map to manually transform the response if you need it or implement a different endpoint. 如果需要,可以使用map手动转换响应,也可以实现其他端点。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM