Issue
I'm initializing my APP_INITIALIZER in app.module.browser.ts but I don't know how to read my environment variable.
@NgModule({
bootstrap: [AppComponent],
imports: [
BrowserModule,
AppModuleShared,
AppAuthModule
],
declarations: [],
providers: [
{ provide: 'BASE_URL', useFactory: getBaseUrl },
{
provide: APP_INITIALIZER, // <-- HERE
useFactory: configurationServiceFactory,
deps: [ConfigurationService],
multi: true
},
{
provide: HTTP_INTERCEPTORS,
useClass: TokenInterceptor, //AuthInterceptor,
multi: true
}
ConfigurationService,
ErrorLogService
]
After initializing, I have some constant variables. I need to read my environment variable to determine the condition to import my module.
import { AccountsComponent } from "./customerList/components/accounts/accounts.component";
import { AccountsPlusComponent } from "./customerList/components/accounts-plus/accounts-plus.component";
let accountsInjection: any = ENV_VARIABLE ? AccountsComponent : AccountsPlusComponent;
@NgModule({
declarations: [
accountsInjection
],
imports: [
...
NOTE: I don't have an app/environment/ folder in my project... (I'm still looking for a way to generate one). I'm using Webpack atm, may I switch to another method for this operation?
Solution
In the case if you want to access variable as env.variable_name
Dotenv is a zero-dependency module that loads environment variables.
Install:
npm install dotenv
Create a .env
file in the root directory of your project. Add environment-specific variables on new lines in the form of NAME=VALUE
. For example:
apiUrl= 'url'
enviorement= 'dev'
in component
require('dotenv').config()
use variables as :
process.env.appUrl,
Answered By - Akj
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.