我有以下服务:
@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;
}
我从这次通话中得到的是:
{ defaultValue: 10, id: 1, name "string" }
但是因为我将类型传递给我的调用,所以我预计只能从此函数中获取名称。 我刚刚开始使用 Angular 4,我不明白有什么问题:/
最佳答案
TypeScript 中的类型是对编译器(也可能是您的 IDE)的静态编译时提示,可在您编写代码时提供帮助。您的服务器无法知道指定的类型,并将一如既往地返回相同的数据。它甚至不知道代码在被调用时是用 TypeScript 编写的。
如果需要,请使用 map
手动转换响应或实现不同的端点。
关于javascript - Angular 4.3 HttpClient GET 类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48101749/