Ngx-admin: How to declare global constant at version 1.0?

Created on 5 May 2017  路  7Comments  路  Source: akveo/ngx-admin

  • I'm submitting a ...
    [ ] bug report
    [ ] feature request
    [ *] question about the decisions made in the repository

in previous version i can set global constant at webpack config files and webpack will replace it when compile.
at version 1.0 it seems not use webpack, then how can i set it?

question

Most helpful comment

o.k example Api-url:

go for dev in the file /src/environments/environment.ts:

export const environment = {
  production: false,
  apiUrl: 'http://dev.env',
};

same for prod in file /src/environments/environment.ts:

export const environment = {
  production: true,
  apiUrl: 'http://prod.env',
};

in your Service/Componente etc:

import { environment } from '../../environments/environment';

@Injectable()
export class ...

this.apiUrl = environment.apiUrl;

dev is now default to use prod with
ng build --env=prod
or
ng serve --env=prod

i hope this is it ?

All 7 comments

yeah. same question here.

i麓m not sure but is theme.configProvider.ts not that you looking for?

nope. that's not what we looking.
For example, we need to set different API_URL_PREFIX in the different environment.
In previous version before 1.0 we set it on webpack.dev.js or webpack.prod.js.
But this is not working after upgraded to version 1.0

Hmm. I think you misunderstand my meaning.
Previously we use webpack DefinePlugin to achieve this purpose.
here's the reference: http://stackoverflow.com/questions/30030031/passing-environment-dependent-variables-in-webpack

how I set it in webpack.dev.js or webpack.prod.js:

new DefinePlugin({
  'ENV': JSON.stringify(METADATA.ENV),
  'HMR': METADATA.HMR,
  'API_URL': JSON.stringify(METADATA.API_URL),
  'process.env': {
    'ENV': JSON.stringify(METADATA.ENV),
    'NODE_ENV': JSON.stringify(METADATA.ENV),
    'HMR': METADATA.HMR,
    'API_URL': JSON.stringify(METADATA.API_URL)
  }
}),

and then we can get the value directly in the application like:

return this.http.get(`${API_URL}/staff`);

but now this approach is not working due to variable undefined.

o.k example Api-url:

go for dev in the file /src/environments/environment.ts:

export const environment = {
  production: false,
  apiUrl: 'http://dev.env',
};

same for prod in file /src/environments/environment.ts:

export const environment = {
  production: true,
  apiUrl: 'http://prod.env',
};

in your Service/Componente etc:

import { environment } from '../../environments/environment';

@Injectable()
export class ...

this.apiUrl = environment.apiUrl;

dev is now default to use prod with
ng build --env=prod
or
ng serve --env=prod

i hope this is it ?

oh. this is another good solution!
Thanks @Newan !

Was this page helpful?
0 / 5 - 0 ratings

Related issues

queirozfcom picture queirozfcom  路  4Comments

xandatspain picture xandatspain  路  3Comments

PatrickHuetter picture PatrickHuetter  路  3Comments

nfdavenport picture nfdavenport  路  3Comments

mignam picture mignam  路  3Comments