HttpClient.get Нет перегрузки соответствуют этому вызову - PullRequest
0 голосов
/ 18 марта 2020

Я хочу иметь возможность загрузить файл (здесь, используя блоб). Для этого мне нужно получить имя файла с сервера, который встроен в заголовок «content-disposition». Вот моя функция:

const header = {Authorization: 'Bearer  ' + token};
const config = {headers: header, responseType: 'blob' as 'blob', observe: 'response' as 'response'};
return this.http.get<HttpResponse<Blob>>(url, config);

Но я получил ошибку:

error TS2345: Argument of type '{ headers: { Authorization: string; }; responseType: "blob"; observe: "response"; }' is not assignable to parameter of type '{ headers?: HttpHeaders | { [header: string]: string | string[]; }; observe?: "body"; params?: HttpParams | { [param: string]: string | string[]; }; reportProgress?: boolean; responseType?: "json"; withCredentials?: boolean; }'.
Types of property 'observe' are incompatible.
Type '"response"' is not assignable to type '"body"'.

Когда я go к определению функции, я получаю:

/**
 * Constructs a `GET` request that interprets the body as a `Blob` and
 * returns the full `HTTPResponse`.
 *
 * @param url     The endpoint URL.
 * @param options The HTTP options to send with the request.
 *
 * @return An `Observable` of the `HTTPResponse` for the request,
 *  with the response body as a `Blob`.
 */
get(url: string, options: {
    headers?: HttpHeaders | {
        [header: string]: string | string[];
    };
    observe: 'response';
    params?: HttpParams | {
        [param: string]: string | string[];
    };
    reportProgress?: boolean;
    responseType: 'blob';
    withCredentials?: boolean;
}): Observable<HttpResponse<Blob>>;

Я не понимаю, что я делаю не так. Можете ли вы помочь мне, пожалуйста?

Спасибо за вашу помощь!

1 Ответ

1 голос
/ 18 марта 2020

Вы используете обобщенную c версию функции, но просматриваете не-обобщенные c документы. Не существует перегрузки generi c, которая принимает observe: 'response', responseType: 'blob'.

Вместо этого используйте версию non-generi c:

const header = {Authorization: 'Bearer  ' + token};
const config = {headers: header, responseType: 'blob' as 'blob', observe: 'response' as 'response'};
return this.http.get(url, config);

Все только перегрузки generi c разрешить responseType: 'json'.

Источник: https://github.com/angular/angular/blob/master/packages/common/http/src/client.ts

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...