2
Based on this example for Http, I would like to know how to instantiate Httpclient:
**constructor() {
const browserXhr: BrowserXhr = new BrowserXhr();
const baseResponseOptions: ResponseOptions = new ResponseOptions();
const xsrfStrategy: CookieXSRFStrategy = new CookieXSRFStrategy();
const backend: XHRBackend = new XHRBackend(browserXhr, baseResponseOptions, xsrfStrategy);
const requestOptions: RequestOptions = new RequestOptions();
const http: Http = new Http(backend, requestOptions);
this._http = http;**
Then I would like to know how to instantiate Httpclient in the same way.
Obs: the shape **constructor(private http: HttpClient) {}**
does not serve me in this case because I need to instantiate within a class typescript and in this way or extending another class and keeps demanding the http as a parameter.
Some more current considerations...
I need to create a class that when instantiated I inform the name of the table that it will work but I don’t want to have to inform more parameters in the class constructor as I have to do currently as this example below (this is how it currently works):
@Injectable()
export class MvsDbTable implements OnInit {
constructor(
@Inject('_tableName') public _tableName: string,
@Inject('_HTTP') public _HTTP: HttpClient ) {}
So I urge the class on a service:
public _tblFinances = new MvsDbTable('Finances', this._Http);
But I would like to not have to enter this parameter ", this.http".
Then I wish it were so:
@Injectable()
export class MvsDbTable implements OnInit {
constructor(
@Inject('_tableName') public _tableName: string ) {
this._HTTP = new HttpClient(this._Handler); }
Only it does not work instantiating the _Handler parameter because it is abstract so it cannot be instantiated.
Then I would instantiate the class in such a service:
public _tblFinances = new MvsDbTable('Finances');
It just cleans up the code, the first way it works. What I try to figure out is how to instantiate Httpclient within the class without having to pass Httpclient as a parameter in the constructor as I did with Http which also worked.
Thank you
Marcelo, sorry for my delay but I had left the project a little aside and I only resumed now, unfortunately I had not seen your answer, I thank you very much for your help.
– Marcelo Varela da Silva
It didn’t work and it seems to me that there is a mix between http and httpclient in your code (unless I’m mistaken). But even if adapting to httpclient just didn’t work, httpclient to be instantiated asks for an Handler parameter, put it and call. GET is not executed. It gives no error or anything, it just generates error exception but without even making the Rest request. So I still need information on how to instantiate an Httpclient without having to inject it into the constructor. !
– Marcelo Varela da Silva
@Marcelovareladasilva sorry the delay to answer. There really was a problem in the sample code. I made the correction and added a few more details. Please make sure to answer your question.
– Marcelo Rodrigues
Thanks again for answering. I made a modification in my question trying to clarify more my need, can review above.
– Marcelo Varela da Silva
I believe that in this your reply the part of requestLoader is the way of what I need but unfortunately I did not have the knowledge of how to implement your suggestion in my service, in the module I implemented right as suggested but could not use in the service, like in this example below that doesn’t work: @Injectable() export class Mvsdbtbl { constructor( @Inject('_tblName') public _tblName: string ) { this. _HTTP = requestLoader(); } Basically what I need is to instantiate Httpclient inside a class without using Dependency Injection. Thank you
– Marcelo Varela da Silva
What if you created a service with a generate method in which it receives the httpclient reference from the constructor and when you call the generate method it gives a new one in the Mvstable class by injecting http? The service would be a kind of Builder to abstract the need to create http client in hand (it would even receive Singleton http from the angular itself)
– Marcelo Rodrigues
@Marcelovareladasilva @Injectable() export class Mvstablebuilderservice { constructor(private http: Httpclient) { } generate(tableName: string) ː Return new Mvsdbtable(tableName, this.http);} }
– Marcelo Rodrigues
Sorry to keep you waiting, I’ve been pretty busy delivering a project. Yes, that’s how it works, the other day I thought about it but since I had another priority I hadn’t tried, now not to leave you unanswered I just tested and it really works, I didn’t want to keep running around but after the http migration (which I could instantiate) httpclient will have to be that way. It was very good, thank you very much for the suggestion. Hug!
– Marcelo Varela da Silva