Angular HttpClient return expecting observable rather than observable

前端 未结 4 875
温柔的废话
温柔的废话 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

    It's strange, don't give error if you write

    GetPortfolio(portfolioId: string): Observable {
        return this.http.get('....', {
            headers: new HttpHeaders(
                {
                    'Content-Type': 'application/json',
                })   
        });
    }
    

    It's look like, the compiler expect an object with the properites headers,params,observe..., but as your object have no type, the compiler can accept it

    even you can do

    headers: HttpHeaders = new HttpHeaders({
            'Content-Type': 'application/json',
        })
    GetPortfolio(portfolioId: string): Observable {
            return this.http.get('...', {
                headers: this.headers
            })
        }
    

提交回复
热议问题