简体   繁体   English

Angular APP_INITIALIZER 应用配置初始化缓慢

[英]Angular APP_INITIALIZER application configuration initialized slowly

I am using Angular 8. My AppModule has config like so我正在使用 Angular 8. 我的 AppModule 有这样的配置

{
provide: APP_INITIALIZER,
useFactory: initConfig,
deps: [AppConfigService],
multi: true,
}

with factory function like与工厂 function 一样

export function initConfig(appConfig: AppConfigService) {
 return () => appConfig.loadConfig();
 }

loadCofig function definition is like this loadCofig function 定义是这样的

  public loadConfig() {
return this.http.get(environment.config,{responseType: 'text'})
  .toPromise()      
  .then((configVal: any) => {          
    this.config = this.extractData(configVal);        
  })
  .catch((err: any) => {        
    console.error(err);
  });
}

we also have this function to retrive the initialized config values throughout the application我们还有这个 function 来检索整个应用程序的初始化配置值

 getConfig() {    
  return this.config;
 }

I am using MSAL library MSAL V1 for angular 8 and i am using platformBrowserDynamic method to initialize我正在为 angular 8 使用 MSAL 库 MSAL V1 ,我正在使用 platformBrowserDynamic 方法进行初始化

The problem is that when i clear cache and load the app loadConfig asynchronous call is taking time, but before that sidebar component is trying to access config of AppConfigService which is still not initalized so first time page load is failing.问题是,当我清除缓存并加载应用程序时, loadConfig异步调用需要时间,但在侧边栏组件尝试访问 AppConfigService 的config之前,该配置仍未初始化,因此第一次页面加载失败。

But after reload everything is working fine.但重新加载后一切正常。

It may not be major issue.这可能不是主要问题。 But still i tried to eliminate this error using following code但我仍然尝试使用以下代码消除此错误

public loadConfig() {
let innerfunc = async  () => {
try {
     let response = await fetch(environment.config);    
     let configtext = await response.text();
     this.config = await this.extractData(configtext);
    }
    catch(err){
      console.error(err);
    }
  }
innerfunc();
}

my routing config looks like this我的路由配置看起来像这样

{
path: 'dashboard',
canActivate: [MsalGuard,AuthService],
loadChildren: () => import('./lazymodules/dashboard/dashboard.module').then(m => 
 m.DashboardModule)
 }

But now with this second implementation strange thing is happening.但是现在随着第二次实施,奇怪的事情发生了。 During very first page load from root paht everything is fine.在从 root paht 加载第一个页面时,一切都很好。 but after that in all page loads getConfig() returns undefined but in former implementation it returns proper config object.It seems like in second implementation of loadConfig, loadConfig is called more than once.It is called at later point after sidebar is trying to access config object. can anyone help me to understand what's happening.但在那之后,在所有页面加载中, getConfig()返回未定义,但在以前的实现中,它返回正确的配置 object。似乎在 loadConfig 的第二次实现中,loadConfig 被调用不止一次。它在侧边栏尝试访问后稍后调用配置 object。任何人都可以帮助我了解发生了什么。 In first implementation How its working properly and executed only one.在第一次实施中,它如何正常工作并只执行一次。 In second implementation why config object is not initalized properly and available through out the app在第二个实现中,为什么配置 object 没有正确初始化并且在整个应用程序中可用

The difference between the first and the second implementation is that in the second one you don't return a promise, try to do something like this第一个和第二个实现的区别在于,在第二个实现中你不返回 promise,尝试做这样的事情

async loadConfig() {
    try {
       let response = await fetch(environment.config);    
       let configtext = await response.text();
       this.config = await this.extractData(configtext);
    } catch(err){
       console.error(err);
    }
  }
}

load config return now a promise and you can use like you wrote before加载配置现在返回 promise 并且您可以像之前写的那样使用

export function initConfig(appConfig: AppConfigService) {
   return () => appConfig.loadConfig();
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM