Could not load the config file in service - vendor js 65401

随声附和 提交于 2020-07-10 10:26:26

问题


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

来源:https://stackoverflow.com/questions/62768885/could-not-load-the-config-file-in-service-vendor-js-65401

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!