It works fine in injecting into components. problem occurs in service injection.
I am using runtime configuration for end points.it works in components.i can able to change in runtime but throws an error when injecting in other services global
export class AppConfigService {
static settings: IAppConfig;
constructor(private http: HttpClient) {}
load() {
const jsonFile = `../assets/config/config.json`;
return new Promise<void>((resolve, reject) => {
this.http.get(jsonFile).toPromise().then((response : IAppConfig) => {
AppConfigService.settings = <IAppConfig>response;
console.log('Config Loaded');
console.log( AppConfigService.settings);
resolve();
}).catch((response: any) => {
reject(`Could not load the config file`);
});
});
}
}
in the app module provide app intializers:
providers: [
AppConfigService,
{ provide: APP_INITIALIZER,useFactory: initializeApp, deps: [AppConfigService], multi: true},
export function initializeApp(appConfigService: AppConfigService) {
return (): Promise<any> => {
return appConfigService.load();
}
}
when integrate with app service have the error. could not load the config file. But it works in a component.
auth.service import { AppConfigService } from 'app/app-config.service';
@Injectable({
providedIn: 'root'
})
export class AuthService {
apiServer = AppConfigService.settings.apiServer;
constructor(private httpClient: HttpClient) {
console.log(this.apiServer.link1);
}
Reference link :reference url
App.settings - the Angular way? Loading configuration files in Angular 6 Load Config JSON File In Angular 2