gpt4 book ai didi

Angular:在 AoT 中提供 Http

转载 作者:可可西里 更新时间:2023-11-01 16:36:21 27 4
gpt4 key购买 nike

我正在使用我提供的自定义 http 客户端作为 providefactory 的依赖项。

{
provide: TranslateLoader,
useFactory: HttpLoaderFactory,
deps: [Client, Url]
},

在 JiT 中工作正常,但在 AoT 中我收到此错误消息:

ERROR in C:/PATH/src/$$_gendir/app/app.module.ngfactory.ts (1,1): Argument of type 'Client' is not assignable to parameter of type 'Http'.
Property '_backend' is missing in type 'Client'.

但这应该无关紧要,因为它受到保护并且是一个不会从外部访问的属性,对吧?

我该怎么办?将那些 protected 属性添加到我的客户端不会解决问题,而且我不知道如何让我的类在不中断的情况下扩展 http,因为在 super 调用中我需要传递一个我不知道它是如何工作的 ConnectionBackend以及如何获得。

编辑:我试过 extend 方法但没有成功:扩展时,如果我有这样的构造函数:

@Injectable()
export class Client extends Http {
constructor(private authService: AuthService, providers: XHRBackend) {
super(providers, null);
this.onInit();
}

或者喜欢

@Injectable()
export class Client extends Http {
constructor(private authService: AuthService) {
super(null, null);
this.onInit();
}

例如将 http 方法覆盖为生成的方法

return super.get(url, this._getOptions(options)).catch((error: any) => this._handleError(error));

它会说:

zone.js:569 Unhandled Promise rejection: Cannot read property 'merge' of null ; Zone: <root> ; Task: Promise.then ; Value: TypeError: Cannot read property 'merge' of null
at mergeOptions (http.es5.js:1767)

最佳答案

添加提供商

    useFactory: (
backend: XHRBackend,
defaultOptions: RequestOptions) =>
new CustomHttp(backend, defaultOptions),
deps: [XHRBackend, RequestOptions]

另见 How to override Http class in RC6?

关于Angular:在 AoT 中提供 Http,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43524106/

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