2

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!

9
  • Why do you create an reflector imperatively? Commented Sep 14, 2016 at 13:26
  • I'm building a library to facilitate the access to my database so that I can use something like this in my components (client-side) this.todoDB = createHttp(url, headers) then this.todoDB.post.... Commented Sep 14, 2016 at 13:29
  • You don't need to create your own injector for this. Commented Sep 14, 2016 at 13:32
  • is there a more elegant way to achieve this? :) Commented Sep 14, 2016 at 13:33
  • Just ask the user to import your module. If you module contains imports: [HttpModule] you should be fine. And add Http to the constructor where you need it constructor(private http:Http) {} Commented Sep 14, 2016 at 13:34

1 Answer 1

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

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

See also Inject Http manually in angular 2

Sign up to request clarification or add additional context in comments.

2 Comments

does it mean as long as HttpModule is imported in the (client-side) AppModule, I don't need to "provide" HTTP_PROVIDERS anymore to inject Http? the code I'm writing is aiming to be a library
You don't need it to inject Http but you need it when you create an injector yourself like you do in your question.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.