gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-29 22:56:06 24 4
gpt4 key购买 nike

从 Angular 7 更新到最新版本 (8) 后,遇到了一些我能够解决的问题,但我找不到解决方案。

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

我的代码:

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

最佳答案

可能你正在从 @angular/common/http/src/params 导入 HttpParamsOptions 接口(interface):

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

这个问题在 Angular 的 Github 存储库上也有讨论: https://github.com/angular/angular/issues/23523 , https://github.com/angular/angular/issues/22013 , https://github.com/angular/angular/issues/20276

问题是 HttpParamsOptions 接口(interface)不是由公共(public) API 导出的,尽管它在作为公共(public) API 一部分的 HttpParams 中使用:

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

当前的解决方法是通过从 Angular 源代码中复制接口(interface),将接口(interface)包含在您自己的代码中,如 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);

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

关于javascript - 更新到 angular 8 后找不到模块@angular/common/http/src/params,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56598019/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com