【问题标题】:I already got environment by useFactory, then how to pass value per environment to AppModule?我已经通过 useFactory 获得了环境,那么如何将每个环境的值传递给 AppModule?
【发布时间】:2021-07-28 13:22:59
【问题描述】:

我在 Angular 11 项目中有 AppModule。

  @NgModule({
  declarations: [
    AppComponent,
    HomeComponent,
    ProfileComponent
  ],
  imports: [
    BrowserModule,
    AppRoutingModule,
    MsalModule.forRoot( new PublicClientApplication({
      auth: {
        clientId: 'Enter_the_Application_Id_here', 
        authority: 'Enter_the_Cloud_Instance_Id_Here'
        redirectUri: 'Enter_the_Redirect_Uri_Here'
      },
      cache: {
        cacheLocation: 'localStorage',
        storeAuthStateInCookie: isIE, // Set to true for Internet Explorer 11
      }
    }), null, null)
  ],
  providers: [MyAppConfig,
  {
      provide: APP_INITITALIZER,
      useFactory: getMyUseFactory,
      deps: [MyAppConfig],
      multi: true
  }],
  bootstrap: [AppComponent]
})
export class AppModule { }
export function getMyUseFactory(c: MyAppConfig) {
   return () => c.load();
}

MyAppConfig中的方法load可以返回json值,例如:

{
   clientId: '12345',
   env: 'dev'
}

{
   clientId: '67890',
   env: 'qa'
}

在管道自动化 CI/CD 中构建代码时设置环境。现在我想把 clientId 放到下面而不是硬编码。

MsalModule.forRoot( new PublicClientApplication({
      auth: {
        clientId: 'Enter_the_Application_Id_here', 

所以我的问题是如何将useFactory 的返回值应用到clientId

【问题讨论】:

标签: angular typescript


【解决方案1】:

自己想办法。

定义一个变量,然后在 getMyUseFactory() 方法中赋值。

const myId = '';
export function getMyUseFactory(c: MyAppConfig) {
     myId = c.env.Id;
     return () => c.load();
}

然后clientId: myId

【讨论】:

    猜你喜欢
    • 2021-10-03
    • 2020-07-26
    • 1970-01-01
    • 2020-03-05
    • 1970-01-01
    • 2021-11-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多