【问题标题】:Angular 10 Upgrade and HTTPClient request properties can no longer be set无法再设置 Angular 10 升级和 HTTPClient 请求属性
【发布时间】:2021-07-24 12:44:03
【问题描述】:

好的,我现在尝试了各种方法,但我迷路了,所以就这样吧。 在我的 Angular 项目中,我正在尝试升级,直到我从 9 升级到 10。

我通过 HttpClient 的请求现在失败了。我有一个基类,它将令牌标头添加到对 Auth 的所有请求以及传入的任何其他请求中,如下所示:

export interface IApiCallerOptions {
    headers?: HttpHeaders;
    params?: HttpParams;
    observe?: 'response' | 'body' | 'events';
    reportProgress?: boolean;
    responseType?: 'arraybuffer'|'blob'|'json'|'text';
    withCredentials?: boolean;
}

private _addAuthHeaderToOptions(options: IApiCallerOptions) {
    var moo = Object.assign({}, options, {
      headers: (options.headers || new HttpHeaders())
      .append('authorization', `bearer ${this._apiLocations.token}`)
    });
    console.log(moo);
    return moo;
  }

get<T>(apiName: ApiNames, apiResource: string, options: IApiCallerOptions = {responseType: 'json'}): Observable<T> {
    const url = this._buildUrl(apiName, apiResource);

    const ops = this._addAuthHeaderToOptions(options);
    var  moo = this._httpClient.get<T>(url, ops);

    return moo;
  }

这不再有效,我收到错误:

重载 1 of 15, '(url: string, options: { headers?: HttpHeaders | { [标题:字符串]:字符串 |细绳[]; };观察:“事件”;参数?: HttpParams | { [参数:字符串]:字符串 |细绳[]; };报告进度?: 布尔值;响应类型?:“json”; withCredentials?:布尔值; }): Observable<...>',给出了以下错误。 'IApiCallerOptions & { headers: HttpHeaders; 类型的参数}' 不能分配给类型为 '{ headers?: HttpHeaders | { [标题:字符串]:字符串 |细绳[]; };观察:“事件”;参数?: HttpParams | { [参数:字符串]:字符串 |细绳[]; };报告进度?: 布尔值;响应类型?:“json”; withCredentials?:布尔值; }'。

我觉得此时我已经蒙蔽了双眼,有人可以确认为什么我可以像以前一样将参数分配给一个对象并将其传递到请求中吗??

【问题讨论】:

标签: angular header httpclient upgrade


【解决方案1】:

已修复。似乎只要类型没有被解释得那么紧,就可以了,因为结构是正确的,与升级前没有什么不同。

只需将“as Object”添加到 Object.assign 即可防止这种强耦合及其编译和再次正常运行。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-01-06
    • 2019-06-09
    • 1970-01-01
    • 1970-01-01
    • 2020-12-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多