2

For some reason, I don't want to use constructor(private http: Http) DI.

Looked at https://angular.io/docs/ts/latest/api/http/index/Http-class.html

and tried

const injector = ReflectiveInjector.resolveAndCreate([
  BaseRequestOptions,
  XHRConnection,
  {
    provide: Http,
    useFactory: (backend, defaultOptions) => new Http(backend, defaultOptions),
    deps: [XHRConnection, BaseRequestOptions]
  }
]);

this.http = injector.get(Http);

Error says

ORIGINAL EXCEPTION: Cannot resolve all parameters for 'XHRConnection'(?, ?, ?). Make sure that all the parameters are decorated with Inject or have valid type annotations and that 'XHRConnection' is decorated with Injectable.

2 Answers 2

1

You need to provide HTTP_PROVIDERS:

const injector = ReflectiveInjector.resolveAndCreate([
  HTTP_PROVIDERS,
  XHRConnection,
  {
    provide: Http,
    useFactory: (backend, defaultOptions) => new Http(backend, defaultOptions),
    deps: [XHRConnection, BaseRequestOptions]
  }
]);
Sign up to request clarification or add additional context in comments.

1 Comment

HTTP_PROVIDERS is now deprecated. Do you have an example for RC.5+?
0

Actually, it can be as simple as

const injector = ReflectiveInjector.resolveAndCreate([
  HTTP_PROVIDERS
]);

this.http = injector.get(Http);

Based on Günter Zöchbauer's answer.

1 Comment

HTTP_PROVIDERS is now deprecated. Do you have an example for RC.5+?

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.