将错误作为类型为“{ headers: HttpHeaders; 的参数” }' 不可分配给 'HttpParamsOptions' 类型的参数

Getting error as Argument of type '{ headers: HttpHeaders; }' is not assignable to parameter of type 'HttpParamsOptions'

所以目前我们的项目是 Angular 版本 4,我们正在尝试将它升级到 Angular 7。当我放入新的 http 模块时,它开始抛出错误:Argument of type '{ headers: HttpHeaders; }' is not assignable to parameter of type 'HttpParamsOptions'.

在此更改之前,我们使用的是 RequestOptions,但现在它已从 Angular 7.

中弃用

代码来自 Angular 4:

   options(): RequestOptions {
     const headers = new Headers();
     headers.append('Content-Type', 'application/json; charset=utf-8');
     let options = new RequestOptions({ headers: headers });
     return options;
   }

代码来自 Angular 7

      options(): HttpParams {
        const headers = new HttpHeaders();
        headers.append('Content-Type', 'application/json; charset=utf-8');
        let options = new HttpParams ({ headers: headers });
        return options;
      }

您将参数放在 headers 中,而现在它们与参数分开:

所以参数和 headers 以不同的方式进行。所以在你的场景中看起来像这样:

      options(): HttpHeaders {
        const headers = new HttpHeaders({'Content-Type', 'application/json; charset=utf-8'});
        return headers ;
      }

请注意,我也从 headers.append 摆脱了

你现在得到的应该是这样的:

headers = options();
this.http.get(url, { headers });

如果您需要参数,请以单独的方式进行:

headers = options();
params = new HttpParams(); 
this.http.get(url, { headers , params});