Reputation: 9686
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.
Upvotes: 0
Views: 284
Reputation: 9686
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;
}
Upvotes: 0