angular2 resolveAndCreate HTTP - 在RC7中缺少HTTP_PROVIDERS [英] angular2 resolveAndCreate HTTP - missing HTTP_PROVIDERS in RC7

查看:137
本文介绍了angular2 resolveAndCreate HTTP - 在RC7中缺少HTTP_PROVIDERS的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

HTTP_PROVIDERS存在时,我可以使用

 导出函数createHTTP(url:string,headers ?:标题){
let injector = ReflectiveInjector.resolveAndCreate([
myHttp,
{provide:'defaultUrl',useValue:url},
{provide:'defaultHeaders' useValue:headers || new Headers()},
... HTTP_Providers
])
return injector.get(myHttp)
}

myHttp是Http的包装器

  @Injectable()
export class myHttp {
constructor(@Inject('defaultUrl)private url:string,@Inject('defaultHeaders)private headers:Headers,private http:Http){}

get()
put()...
}

现在HTTP_PROVIDERS已被弃用和删除,我该如何提供?



谢谢!

解决方案

  @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},
{提供:ResponseOptions,useClass:BaseResponseOptions},
XHRBackend,
{provide:XSRFStrategy,useFactory:()=>新的CookieXSRFStrategy()},
];


back in RC4 when HTTP_PROVIDERS existed I could create my custom http instance using

export function createHTTP(url:string, headers?:Headers){
  let injector = ReflectiveInjector.resolveAndCreate([
    myHttp,
    {provide:'defaultUrl', useValue:url},
    {provide:'defaultHeaders', useValue:headers || new Headers()},
    ...HTTP_Providers
  ])
  return injector.get(myHttp)
}

myHttp was a wrapper for Http

@Injectable()
export class myHttp{
  constructor(@Inject('defaultUrl) private url:string, @Inject('defaultHeaders) private headers:Headers, private http:Http){}

  get()
  put()...
}

Now with HTTP_PROVIDERS deprecated and removed, how do I provide it?

Thanks!

解决方案

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

or copy the definition of HTTP_PROVIDERS from the Angular2 source to your source and use it there like before.

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

这篇关于angular2 resolveAndCreate HTTP - 在RC7中缺少HTTP_PROVIDERS的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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