1
When starting the application, the function is executed first getBanners()
of the service banners.service.ts
and only then is the this.headers
of the service app.service.ts
.
If I switch pages, it works normally. The error only occurs when starting the application.
I want the banners.service.ts
perform the function only after this.headers
of the service app.service.ts
has been amended.
banners.service.ts
getBanners(){
return this._http.get(this.appService.endpoint + 'banners', { headers: this.appService.headers })
.toPromise()
.then((data) => {
data = data.json();
return data;
});
}
app.service.ts
constructor() {
this.getContentType().then(data => {
this.headers.set('Content-Type', data);
});
}
private getContentType(): Promise<any> {
return 'application/json';
}
Shouldn’t the name of Shared.service.ts and the added service in the Appmodule Provider be Sharedservice instead of Appservice? However, even changing these names, I gave a.log() console of getHead() and on header: he has only the proto
– Rodrigo Pauletti
.....Went well???
– Lollipop
It didn’t work. On the console.log(this.sharedService.getHead()) it returns me body, headers, etc., but the headers only lists the proto instead of also listing 'Content-Type' and 'Authorization'
– Rodrigo Pauletti
Also, I currently have the getBanners() function on banners.service.ts. Is it necessary to change the code for the banners.components.ts like you did in your example? Sorry, I’m still not very familiar with Angular.
– Rodrigo Pauletti
And the
this.appService.getHead()
? you have to use it. Log on it...– Lollipop
But getHead() is in Sharedservice, not Appservice...
– Rodrigo Pauletti
What you don’t understand... The appService is in the
constructor
ofappService
, so you can use thethis.appService.getHead()
which belongs to Sharedservice– Lollipop
You can’t use your own service on your own
constructor
– Rodrigo Pauletti
The goal of your question is to generate a global variable, accessible to other Komponents, so the best way is this... So you don’t need to do particularly on each.
– Lollipop
The function
getHead()
is in Sharedservice. How would you use it in Appservice without importing Sharedservice?– Rodrigo Pauletti
The
AppService
shouldn’t have aextends SharedService
?– Rodrigo Pauletti