0

How do I instantiate an Http instance using ReflectiveInjector?

Http has a constructor that takes a ConnectionBackend and a RequestOptions. But ConnectionBackend is an abstract class so how on earth does the injector know what to resolve it with? Any tips on resolving RequestOptions will also be gratefully appreciated.

In http.d.ts Http is decorated with neither @Component nor @Injectable which according to the Angular documentation is required for a type to be injectable at all. Http is certainly injectable but does not seem to have any of the infrastructure in place to make it so.

jonrsharpe
  • 115,751
  • 26
  • 228
  • 437
Neutrino
  • 8,496
  • 4
  • 57
  • 83
  • "There seems to be a bit of a gap between the documentation and the reality." Welcome to the wonderful world of modern front-end development. :) – Stan May 05 '17 at 15:05
  • See http://stackoverflow.com/questions/39243525/inject-http-manually-in-angular-2, http://stackoverflow.com/questions/39491647/angular2-resolveandcreate-http-missing-http-providers-in-rc7/39491672#39491672 – Günter Zöchbauer May 05 '17 at 15:08
  • Possible duplicate of [Angular2 RC6 HttpModule manual injection](http://stackoverflow.com/questions/39406933/angular2-rc6-httpmodule-manual-injection) – jonrsharpe May 05 '17 at 15:09
  • Specifically http://stackoverflow.com/a/39503251/3001761. *"ConnectionBackend is an abstract class so how on earth does the injector know what to resolve it with?"* - you tell it, that's what `{ provide: ... }` does. – jonrsharpe May 05 '17 at 15:09
  • But normally you do not need to tell Angular what type of ConnectionBackend is required to instantiate an Http instance. You just mark a class as @Injectable and give its constructor a 'private http: Http' argument and it works it out for itself. So the question remains, how does it usually work this out for itself? – Neutrino May 05 '17 at 15:12
  • That's what the `HttpModule` does, it [provides `XHRBackend`](https://github.com/angular/angular/blob/master/packages/http/src/http_module.ts#L49). – jonrsharpe May 05 '17 at 15:14

1 Answers1

0

Yes my question is the same as the one other people are linking, but the selected answer in that question didn't actually answer the question. I did find the answer elsewhere in that but I'm going to copy it here and mark it as the answer so that people asking the same question will at least find it marked as the answer somewhere.

import { ReflectiveInjector } from '@angular/core';
import {
  Http, BrowserXhr, RequestOptions, BaseRequestOptions, ResponseOptions, BaseResponseOptions, ConnectionBackend,
  XHRBackend, XSRFStrategy, CookieXSRFStrategy
} from '@angular/http';


export class HttpFactory {
  public static Init(): void {
    HttpFactory._injector = ReflectiveInjector.resolveAndCreate([
      Http,
      BrowserXhr,
      { provide: RequestOptions, useClass: BaseRequestOptions },
      { provide: ResponseOptions, useClass: BaseResponseOptions },
      { provide: ConnectionBackend, useClass: XHRBackend },
      { provide: XSRFStrategy, useFactory: () => new CookieXSRFStrategy() }
    ]);
  }

  public static CreateHttp(): Http {
    return HttpFactory._injector.get(Http);
  }

  private static _injector: ReflectiveInjector;
}
Neutrino
  • 8,496
  • 4
  • 57
  • 83