首页 > 解决方案 > Angular v8 中的 HTTP 发布并出现打字稿错误

问题描述

我目前收到一条错误消息,上面写着.... event.id 未知

“'HttpResponse' 类型上不存在属性 'id'。”

我有以下界面:

interface UploadPostResponse {
 id: string;
}

这是我的 POST 调用...配置只是 POST 和一些附加到它的参数

return this.httpClient.request( config )
                  .subscribe(event => {
                    if (event instanceof HttpResponse) {
                      this.router.navigate(['/inbox'], { queryParams: { inboxName: event.id } });
                    }
                  });

我不确定将“UploadPostResponse”接口放在哪里,这样收件箱名称的 event.id 就可以了。

编辑:

const config = new HttpRequest('POST', `${postUrl}`,{file: inbox});

标签: angulartypescripthttppost

解决方案


响应负载位于响应的body属性中。你应该idevent.body.id.

return this.httpClient.request( config )
  .subscribe(event => {
    if (event instanceof HttpResponse) {
      this.router.navigate(['/inbox'], { queryParams: { inboxName: event.body.id } });
    }
  });

如果使用,您将直接查询返回的对象httpClient.post

return this.httpClient.post(postUrl, {file: inbox})
  .subscribe((response: any) => {
    this.router.navigate(['/inbox'], { queryParams: { inboxName: response.id } });
  });

推荐阅读