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

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

问题描述

回到 RC4,当 HTTP_PROVIDERS 存在时,我可以使用

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 是 Http 的包装器

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()...
}

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

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

谢谢!

推荐答案

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

或将 HTTP_PROVIDERS 的定义从 Angular2 源复制到您的源,然后像以前一样在那里使用它.

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

你也可以使用这些提供者自己创建一个注入器

You can also create an injector yourself using these providers like

let resolvedProviders = ReflectiveInjector.resolve(HTTP_PROVIDERS);
let injector = ReflectiveInjector.fromResolvedProviders(resolvedProviders, /* this.injector (parent injector if any) */ );
var http = child.get(Http);

另见在angular 2中手动注入Http

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

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