angular - httpClient GET 返回类型

标签 angular angular-httpclient

如果您查看 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/

相关文章:

angular - 在功能模块中导入 HttpClientModule

javascript - Angular 2过早加载组件

ng-repeat 上的 Angular 4 数据绑定(bind)

Angular HttpClient 为零响应返回空值

rxjs - 无法从Interceptor订阅数据

angular - 如何在Angular 8中显示超时错误

angular - 如何在 Angular 6 服务中发出 Http 请求

angular - 如何将图标添加到 mat-table 中的 mat-sort-header

html - 将类添加到 Angular2 组件 HTML 标记不应用样式

javascript - 如何使用 *ngFor 循环的 Angular 在表的 <td> 字段内使用 *ngIf 条件?