-->

Angular 5 Use runtime environment variables in app

2019-08-20 11:20发布

问题:

I have a 3rd party module that I am using for authentication- OktaAuthModule.

For it to be imported in my root module (app.module.ts), it needs to first be configured like this-

const config = {
   url: https://myurl.com/
}

@NgModule({
   declarations: [ ... ],
   imports: [
      OktaAuthModule.initAuth(config),
   ],
   ...
});

I need to produce a single build that can be deployed to multiple runtimes, each having a different config for this module.

Using the built-in "ng build --env " works fine, but requires multiple builds. The APP_INITIALIZER hooks seem to run after this "imports" array has been processed.

I was hoping to make an API call that could pull this information each time the app is deployed. I don't have any idea where I can add this code in the app.module.ts file?

Thanks in advance for any advice / suggestions.

回答1:

I came across similar issue when co figuring Bamboo deployment plans for different environments. Every env had to had dedicated build. I didnt like that much so I have ended up in automated editing of main.js. Environment variables are embedded there so you could modify that per enc just like I did.



标签: angular okta