本文介绍了在Angular 8版本中,对于HttpClient获取请求,HttpObserve是否消失了?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发Angular 8应用程序.我正在尝试根据文档实施HttpClient获取请求: https://angular.io/api/common/http/HttpClient .

I am developing an Angular 8 application. I am trying to implement an HttpClient get request according to the documentation: https://angular.io/api/common/http/HttpClient.

因此,在全球范围内,我尝试使用get(url: string, httpHeader: HttpHeaders, observe: HttpObserve, params : HttpParams)请求,但HttpObserve似乎消失了.你能帮我吗?

So globally I am trying to use a get(url: string, httpHeader: HttpHeaders, observe: HttpObserve, params : HttpParams) request, but it seems that HttpObserve disappeared. Can you help me?

推荐答案

感谢您的回答和评论.当我想添加其他代码时,我回答问题而不是添加注释.我这样做的主要目的是向请求中添加HttpParams(或代表我的get请求中所有附加参数的对象).这是服务方法:

Thank you for your answer and comment. As I want to add additional code, I answer the question rather than add a comment. My main intention doing such a request is to add HttpParams (or rather an object representing all additional parameters to my get request) to the request. Here is the service method:

  getListProduitImmobilierDTO(pagesize: number, page: number, params: HttpParams): Observable<ProduitImmobilierDTO[]> {
    const headerDict = {
      'Content-Type': 'application/json',
      Accept: 'application/json',
      'Accept-Charset': 'charset=UTF-8',
      'Access-Control-Allow-Headers': 'Content-Type'
    };
    return this.http.get('/api/produitimmobilier/all/' + pagesize + '/' + page, new HttpHeaders(headerDict), { observe: 'response' }, params).pipe(map((jsonArray: any) => jsonArray.map((jsonItem: object) => ProduitImmobilierDTO.fromJson(jsonItem))));
  }

您可以看到yazantahhan,我在HttpObserve的位置添加了{watch:'response'},它指示以下错误:

As you can see yazantahhan, I added { observe: 'response' } in place of HttpObserve, and it indicates the following error:

Expected 1-2 arguments, but got 4.

对不起,alt255,但是我无法提供可复制的示例,因为它是一个请求,并且需要服务器来响应该请求.我可以告诉您的是以下方法效果很好:

Sorry alt255, but I can't provide a reproducible example as it is a request and it needs a server to respond to the request. What I can tell you is the following method works well:

  getListProduitImmobilierDTO(pagesize: number, page: number): Observable<ProduitImmobilierDTO[]> {
    const headerDict = {
      'Content-Type': 'application/json',
      Accept: 'application/json',
      'Accept-Charset': 'charset=UTF-8',
      'Access-Control-Allow-Headers': 'Content-Type'
    };

    const requestOptions = {
      headers: new HttpHeaders(headerDict)
    };
    return this.http.get('/api/produitimmobilier/all/' + pagesize + '/' + page, requestOptions).pipe(map((jsonArray: any) =>jsonArray.map((jsonItem) => ProduitImmobilierDTO.fromJson(jsonItem))));
}

这篇关于在Angular 8版本中,对于HttpClient获取请求,HttpObserve是否消失了?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-29 01:49