【问题标题】:Angular 4 Call HttpResponse inside the component组件内的Angular 4调用HttpResponse
【发布时间】:2019-01-11 02:48:46
【问题描述】:

我正在尝试从我的帖子中获取角度为 4 的响应。这是我的代码:

app.component.html:

`findAccordiSmall(pagination: Pagination) {
        this.accordiListTableLoading = true;
        debugger;
        this.accordiService.findAccordiSmall(pagination).subscribe(
          (res: HttpResponse<any>) => {
            res.headers.get('offset');
            res.headers.get('count');

            this.agreementList= res.body;

          }      errors => {
        this.accordiListTableLoading = false;
        Utils.notifyErrors(errors, this.notificationsService);
      }
    );

服务.ts

findAccordiSmall(pagination: Pagination): Observable<HttpResponse<any>> {

    const queryParams = this.getHttpParams(pagination).toString();

    return this.http.post(`${this.ENDPOINT}/agreements-paginated?${queryParams}`,pagination, { observe: 'response' })
        .catch(error => Utils.handleError(error, this.router, 'Errore nel recupero delle pratiche'));
}

POST 正在向我发送我想要的所有数据(标题、响应等),但我无法在我的组件中使用这些值。我怎么称呼他们? 我必须填写: agreementList: Agreement[]; 回复。

抱歉,如果您需要更多信息,请告诉我。

编辑:This is my response

编辑2:

组件.ts

  findAccordiSmall(pagination: Pagination) {
    this.accordiListTableLoading = true;
    this.accordiService.findAccordiSmall(pagination).subscribe(
      (res: Agreement[]) => {
        this.agreementList= res;
      },
  errors => {
    this.accordiListTableLoading = false;
    Utils.notifyErrors(errors, this.notificationsService);
  }
);

service.ts

 findAccordiSmall(pagination: Pagination): Observable<Agreement[]> {

        const queryParams = this.getHttpParams(pagination).toString();
        debugger;
        return this.http.post<Agreement[]>(`${this.ENDPOINT}/agreements-paginated?${queryParams}`,pagination, { observe: 'response' })
            .map(res => res)
            .catch(error => Utils.handleError(error, this.router, 'Errore nel recupero delle pratiche'));}

【问题讨论】:

  • 你使用HttpClient还是Http?
  • 你能说明你收到了什么回复吗?
  • 我正在使用 HttpClient

标签: angular typescript httpresponse


【解决方案1】:

如果您需要用响应填充agreementList: Agreement[],您只需将res(服务函数返回的值)分配给它。无需使用res.body。 你有this.agreementList= res.body;。这是错误的。 此行应为this.agreementList = res;

编辑:更新的组件代码:

findAccordiSmall(pagination: Pagination) {
    this.accordiListTableLoading = true;
    debugger;
    this.accordiService.findAccordiSmall(pagination).subscribe(
      (res: Agreement[]) => {
        this.accordiListTableLoading = false;
        this.agreementList= res;
      },
  errors => {
    this.accordiListTableLoading = false;
    Utils.notifyErrors(errors, this.notificationsService);
  }
);

我用正确的返回值更新了你的服务函数,你还需要map

findAccordiSmall(pagination: Pagination): Observable<Agreement[]> {

const queryParams = this.getHttpParams(pagination).toString();

return this.http.post<Agreement[]>(`${this.ENDPOINT}/agreements-paginated?${queryParams}`,pagination, { observe: 'response' })
    .map(res => res)
    .catch(error => Utils.handleError(error, this.router, 'Errore nel recupero delle pratiche'));

}

所以现在它返回Observable&lt;Agreement[]&gt;,这意味着当您订阅它时,订阅中的res 将从Observable, and it's type will beAgreement[]` 中解包出来

subscribe((res: Agreement[]) =&gt; {})

【讨论】:

  • 如果我使用 this.agreementList = res;出现错误:“类型 HttpResponse 不可分配给类型 Agreement[],类型 HttpResponse 缺少属性 'length'。
  • 在这种情况下,(res: HttpResponse&lt;any&gt;) =&gt; { 这一行应该是(res: HttpResponse&lt;Agreement[]&gt;) =&gt; {,一切都会好起来的。
  • 谢谢你的回复,但我仍然有同样的错误:)
  • 当然——我的错误,res 不再包含在HttpResponse 中。所以该行应该是 (res: Agreement[]) =&gt; { 我用完整的组件代码更新了我的答案。
  • 非常感谢您的帮助!现在一切正常!我欠你的债:)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2018-11-18
  • 2018-04-14
  • 1970-01-01
  • 2018-02-07
  • 2017-09-05
  • 2019-01-24
  • 1970-01-01
相关资源
最近更新 更多