How do you instantiate an HttpClient in Angular 5? I need to instantiate it inside the constructor, but not in this line constructor(private _Http: HttpClient)
.
Maybe something like this hypothetical example:
import { HttpClient, HttpHandler } from '@angular/common/http';
private _Http: HttpClient;
private _httpHandler: HttpHandler;
@Injectable()
export class DataService {
constructor() {
this._Http = new HttpClient(_httpHandler);
}
}
Thank you
As previous answers stated, it's better to use dependency injection. That's being said, you can create instance manually like below
const httpClient = new HttpClient(new HttpXhrBackend({
build: () => new XMLHttpRequest()
}));
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With