如何在不使用构造函数DI的情况下创建Http实例? [英] How to create an Http instance without using constructor DI?

查看:159
本文介绍了如何在不使用构造函数DI的情况下创建Http实例?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

For some reason, I don't want to use constructor(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);

错误说


原始异常:无法解析'XHRConnection'(?,?,?)的所有参数。确保所有参数都使用Inject进行修饰或具有有效的类型注释,并且'XHRConnection'使用Injectable进行修饰。

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]
  }
]);

这篇关于如何在不使用构造函数DI的情况下创建Http实例?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆