Reputation: 151
I had an issue here and I did someting but i would like to know if it's the good way to do it...
I have an interface:
export interface ContexteEnvironnement {
langue: string;
urlPing: string;
urlFinSession: string;
urlTimeout: string;
urlRetour: string;
urlBaseEpv: string;
}
I have a service:
contexteEnvironnement: ContexteEnvironnement;
getDataInitialisation(): Observable<ContexteEnvironnement> {
return this.http.get<ContexteEnvironnement>(configUrl, headerGET);
}
And I have my app component where I want to get datas:
ngOnInit() {
this.initialiserProprietes();
}
initialiserProprietes(): void {
this._serviceConfigInitiale.getDataInitialisation().subscribe(data => {
this._serviceConfigInitiale.dataInitialisation = data;
this._langueService.setLangueCourante(data.contexteEnvironnement.langue);
});
}
I had an error saying:
ERROR TypeError: Cannot read property 'urlBaseEpv' of undefined
So I decided to initialize my variable in my service:
contexteEnvironnement: ContexteEnvironnement = {
langue: '',
urlPing: '',
urlFinSession: '',
urlTimeout: '',
urlRetour: '',
urlBaseEpv: ''
};
And everything works. Is there a better way to do it ?
Thanks a lot for your advices!
Upvotes: 0
Views: 86
Reputation: 674
use classes like this :
export class ContexteEnvironnement {
langue = '';
urlPing = '';
urlFinSession = '';
urlTimeout = '';
urlRetour = '';
urlBaseEpv = '';
}
and then write this in your service or any where else:
contexteEnvironnement = new ContexteEnvironnement();
Upvotes: 1