更新到angular 8后找不到模块@ angular / common / http / src / params

时间:2019-06-14 12:15:39

标签: javascript angular

从Angular 7更新到最新版本(8)之后,我可以解决一些问题,但找不到解决方案。

Cannot find module '@angular/common/http/src/params'.

我的代码:

        const options: HttpParamsOptions = {
            fromObject: params,
            encoder: this.httpParamEncoder
        };

2 个答案:

答案 0 :(得分:1)

@StepUp的答案在Angular v8中不起作用,因为它会引发以下错误:

Module '"node_modules/@angular/common/http"' has no exported member 'HttpParamsOptions'.

StackBlitz:https://stackblitz.com/edit/angular-7udeje


可能是从HttpParamsOptions导入@angular/common/http/src/params界面:

import { HttpParamsOptions } from '@angular/common/http/src/params';

在Angular的Github存储库中也讨论了此问题: https://github.com/angular/angular/issues/23523https://github.com/angular/angular/issues/22013https://github.com/angular/angular/issues/20276

问题是HttpParamsOptions接口不是由公共API导出的,尽管它在公共API的HttpParams中使用:

export declare class HttpParams {
    constructor(options?: HttpParamsOptions);
    ...
}

当前的解决方法是通过从Angular源代码复制接口来将接口包含在您自己的代码中,如https://github.com/angular/angular/blob/8a0e45826a4f140e9330d57594c338276a151401/packages/common/http/src/params.ts#L76

/** Options used to construct an `HttpParams` instance. */
export interface HttpParamsOptions {
   /**
    * String representation of the HTTP params in URL-query-string format. Mutually exclusive with
    * `fromObject`.
    */
   fromString?: string;

   /** Object map of the HTTP params. Mutually exclusive with `fromString`. */
   fromObject?: {
      [param: string]: string | string[];
   };

   /** Encoding codec used to parse and serialize the params. */
   encoder?: HttpParameterCodec;
}

然后按如下所示初始化http参数:

const httpParamsOptions: HttpParamsOptions = { fromObject: { aaa: '111', bbb: '222' } };
const httpParams = new HttpParams(httpParamsOptions);

具有完整代码:https://stackblitz.com/edit/angular-cizc36的StackBlitz演示。

答案 1 :(得分:0)

As Angular documentation says命名空间已更改:

import { HttpParamsOptions } from '@angular/common/http'

更新:

npm install @angular/common