出于某种原因,我不想使用构造函数(私有(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/