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

问题描述

我正在开发一个 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,我添加了 {observe:'response'} 代替了 HttpObserve,它指示了以下错误:

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))));
}

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

08-26 00:12