在 app.module.ts 加载 (Angular) 之前调用函数获取 JSON bject

Calling a function to get JSON bject before app.module.ts Loads (Angular)

我的 app.module.ts 文件是:-

import { MsalModule, MsalInterceptor } from '@azure/msal-angular';
import getConfig from './env';
var config_values = getConfig(); // we are calling the function here, but it takes time to complete
console.log("here",config_values);

const isIE = window.navigator.userAgent.indexOf('MSIE ') > -1 || window.navigator.userAgent.indexOf('Trident/') > -1;

@NgModule({
  declarations: [
  ...
  ...
  ...
  ],
  imports: [
    ...
    ...
    ...
    MsalModule.forRoot({
      auth: config_values,// we want the json values here which are returned by the above function.
      cache: {
        cacheLocation: 'localStorage',
        storeAuthStateInCookie: isIE, // Set to true for Internet Explorer 11
      },
    }, {
      popUp: !isIE,
      consentScopes: [
        'user.read',
        'openid',
        'profile',
      ],
      unprotectedResources: [],
      protectedResourceMap: [
        ['https://graph.microsoft.com/v1.0/me', ['user.read']]
      ],
      extraQueryParameters: {}
    })
  ],
  providers: [
    { provide : HTTP_INTERCEPTORS , useClass : htt, multi : true },
    { provide: HTTP_INTERCEPTORS,useClass: MsalInterceptor,multi: true},
    GlobalsService
  ],
  bootstrap: [component]
})
export class AppModule {

 }

Environment.ts 文件是:-

export function getConfig() {​​​​​
    try{
    let request = new XMLHttpRequest();
    var config;
    request.open('GET',"URL", false);
    request.send(null);

    if (request.status === 200) {​​​​​
        console.log(request.responseText);
        config = request.responseText;
    }​​​​​
    return JSON.parse(config);
}

    catch(e){
        console.log(e);
    }
}

export default getConfig;

由于是外部调用,获取数据需要时间,但是模块加载之前抛出错误,所以我们想要一个解决方案,要么等待外部数据加载,要么在获取数据后加载数据和 SSO 工作正常。 我们想在文件加载之前获取配置值。 任何人都可以帮忙吗!?

您可以使用 platformBrowserDynamic 动态获取 MSAL 的配置值。参见:https://github.com/AzureAD/microsoft-authentication-library-for-js/blob/dev/lib/msal-angular/docs/v1-docs/configuration.md#platformbrowserdynamic