Angular HttpClient return expecting observable rather than observable

前端 未结 4 881
温柔的废话
温柔的废话 2020-12-08 14:54

I\'m getting a compilation error on the return type when using HttpClient. In my function GetPortfolio, I\'m expecting the GET call to return the j

4条回答
  •  误落风尘
    2020-12-08 15:38

    Typecast your httpOptions

    private httpOptions: {
        headers: HttpHeaders
    };
    

    The typescript compiler is pulling the wrong get method type (src)

    /**
    * Construct a GET request which interprets the body as JSON and returns the full event stream.
    *
    * @return an `Observable` of all `HttpEvent`s for the request, with a body type of `T`.
    */
    get(url: string, options: {
        headers?: HttpHeaders | {[header: string]: string | string[]},
        observe: 'events',
        params?: HttpParams|{[param: string]: string | string[]},
        reportProgress?: boolean,
        responseType?: 'json',
        withCredentials?: boolean,
    }): Observable>;
    

    When you specify the type with headers, it pulls the correct type. (src)

    /**
    * Construct a GET request which interprets the body as JSON and returns it.
    *
    * @return an `Observable` of the body as type `T`.
    */
    get(url: string, options?: {
        headers?: HttpHeaders | {[header: string]: string | string[]},
        observe?: 'body',
        params?: HttpParams|{[param: string]: string | string[]},
        reportProgress?: boolean,
        responseType?: 'json',
        withCredentials?: boolean,
    }): Observable;
    

提交回复
热议问题