angular2 resolveAndCreate HTTP - 在RC7缺失HTTP_PROVI

2019-05-12 10:43发布

早在RC4时HTTP_PROVIDERS存在,我可以用我的创建自定义HTTP实例

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包装

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

  get()
  put()...
}

现在,随着HTTP_PROVIDERS否定并取消,我怎么规定的?

谢谢!

Answer 1:

@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);

也参见在角2手动注入的Http



文章来源: angular2 resolveAndCreate HTTP - missing HTTP_PROVIDERS in RC7