无法在服务中加载配置文件 - 供应商 js 65401 [英] Could not load the config file in service - vendor js 65401
问题描述
它在注入组件方面效果很好.服务注入出现问题.
我正在为端点使用运行时配置.它在组件中工作.我可以在运行时进行更改,但在注入其他全局服务时会引发错误
导出类 AppConfigService {静态设置:IAppConfig;构造函数(私有 http:HttpClient){}加载() {const jsonFile = `../assets/config/config.json`;return new Promise((resolve, reject) => {this.http.get(jsonFile).toPromise().then((response : IAppConfig) => {AppConfigService.settings = <IAppConfig>response;console.log('配置加载');console.log( AppConfigService.settings);解决();}).catch((response: any) => {拒绝(`无法加载配置文件`);});});}
}
在应用模块中提供应用初始化器:
提供者:[应用配置服务,{提供:APP_INITIALIZER,useFactory:initializeApp,deps:[AppConfigService],multi:true},导出函数 initializeApp(appConfigService: AppConfigService) {return(): Promise=>{返回 appConfigService.load();}}
与应用服务集成时出现错误.无法加载配置文件.但它适用于组件.
认证服务从'app/app-config.service'导入{ AppConfigService};
@Injectable({提供在:'根'})导出类 AuthService {apiServer = AppConfigService.settings.apiServer;构造函数(私有 httpClient:HttpClient){console.log(this.apiServer.link1);}
参考链接:参考网址
App.settings - Angular 方式?在 Angular 6 中加载配置文件在 Angular 2 中加载配置 JSON 文件
我给你加了一个例子:
https://stackblitz.com/edit/angular-pkuwef?file=src/app/app.module.ts
https://github.com/misanche/angular-pkuwef/>
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
Hi I have added you an example:
https://stackblitz.com/edit/angular-pkuwef?file=src/app/app.module.ts
https://github.com/misanche/angular-pkuwef/
这篇关于无法在服务中加载配置文件 - 供应商 js 65401的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!