gpt4 book ai didi

http - 如何在不使用构造函数 DI 的情况下创建 Http 实例?

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

出于某种原因,我不想使用构造函数(私有(private) http: Http)DI。

看了https://angular.io/docs/ts/latest/api/http/index/Http-class.html

尝试过

const injector = ReflectiveInjector.resolveAndCreate([
BaseRequestOptions,
XHRConnection,
{
provide: Http,
useFactory: (backend, defaultOptions) => new Http(backend, defaultOptions),
deps: [XHRConnection, BaseRequestOptions]
}
]);

this.http = injector.get(Http);

错误说

ORIGINAL EXCEPTION: Cannot resolve all parameters for 'XHRConnection'(?, ?, ?). Make sure that all the parameters are decorated with Inject or have valid type annotations and that 'XHRConnection' is decorated with Injectable.

最佳答案

您需要提供HTTP_PROVIDERS:

const injector = ReflectiveInjector.resolveAndCreate([
HTTP_PROVIDERS,
XHRConnection,
{
provide: Http,
useFactory: (backend, defaultOptions) => new Http(backend, defaultOptions),
deps: [XHRConnection, BaseRequestOptions]
}
]);

关于http - 如何在不使用构造函数 DI 的情况下创建 Http 实例?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39209217/

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