如果您查看 getData() 方法,它会显示 this.http.get<Post>
.是 <Post>
正在指定返回类型、检查响应类型或转换返回的内容
interface Post {
title: string;
body: string;
};
// ...
constructor(private http: HttpClient) {}
getData() {
this.http.get<Post>(this.url).subscribe(res => {
this.postTitle = res.title;
});
}
最佳答案
这是编译器和您选择的 IDE 的类型提示。不会检查类型。
来自documentation :
The HttpClient.get() method parses the JSON server response into the anonymous Object type. It doesn't know what the shape of that object is.
You can tell HttpClient the type of the response to make consuming the output easier and more obvious.
关于angular - httpClient GET 返回类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51968434/