简体   繁体   中英

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" }

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 :/

Types in TypeScript are static compile-time hints to the compiler (and possibly your IDE) to help you while writing code. 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.

Use map to manually transform the response if you need it or implement a different endpoint.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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