I'm trying to understand how to load configuration files in Angular 6, I currently have a ConfigureService with the following code for loading the file:
configUrl = '../assets/config.json';
config: Config;
load() {
console.log('loading configuration');
this.getConfig().subscribe(
(data: Config) => {
console.log('Config request success');
(this.config = data);
console.log(this.config)
}
);
}
private getConfig() {
return this.http.get<Config>(this.configUrl);
}
Where load is called in the constructor of ConfigureService
I use this service to get my Url strings for my api consumer services, and it does run successfully (the console.logs show in the dev window), just not in time for those services to use their respctive url strings in their OnInit methods, and the other services throw errors as they are trying to pull the strings from an undefined config object.
I have tried to read them in by specifying them in the app.module and loading them before startup as seen here:
https://gist.github.com/fernandohu/122e88c3bcd210bbe41c608c36306db9
Yet when I try that method, it tells me that the files could not be found on the server, and the error in the console returns 404, even though the path specified is correct.
How can I ensure that the config service runs first so that other services who are dependent on it do not try to retrieve data before it has finished its initialization?