angular2 resolveAndCreate HTTP - 在RC7中缺少HTTP_PROVIDERS

时间:2016-09-14 13:23:24

标签: http angular typescript dependency-injection

当HTTP_PROVIDERS存在时,

返回RC4我可以使用

创建我的自定义http实例
@Injectable()
export class myHttp{
  constructor(@Inject('defaultUrl) private url:string, @Inject('defaultHeaders) private headers:Headers, private http:Http){}

  get()
  put()...
}

myHttp是Http

的包装器
{{1}}

现在弃用并删除了HTTP_PROVIDERS,我该如何提供呢?

谢谢!

1 个答案:

答案 0 :(得分:3)

@NgModule({
  imports: [HttpModule],
  ...
})
class AppModule {}

或将HTTP_PROVIDERS的定义从Angular2源复制到您的源代码,并像以前一样在那里使用它。

const HTTP_PROVIDERS = [
    {provide: Http, useFactory: 
      (xhrBackend: XHRBackend, requestOptions: RequestOptions): Http =>
          new Http(xhrBackend, requestOptions), 
          deps: [XHRBackend, RequestOptions]},
    BrowserXhr,
    {provide: RequestOptions, useClass: BaseRequestOptions},
    {provide: ResponseOptions, useClass: BaseResponseOptions},
    XHRBackend,
    {provide: XSRFStrategy, useFactory: () => new CookieXSRFStrategy()},
];

您也可以使用

这些提供程序自行创建注射器
let resolvedProviders = ReflectiveInjector.resolve(HTTP_PROVIDERS);
let injector = ReflectiveInjector.fromResolvedProviders(resolvedProviders, /* this.injector (parent injector if any) */ );
var http = child.get(Http);

另见Inject Http manually in angular 2