0

I have an Angular project where I can build the project for a given environment and run it. I've realized this bakes the environment settings into the built project. Now I want to load the environment-specific settings at run time.

So, I've currently got this:

import { EnvConfig } from '../../../../../tools/env/env-config.interface';

export const Config: EnvConfig = JSON.parse('<%= ENV_CONFIG %>');

Where <%= ENV_CONFIG %> gets replaced with the baked environment settings. Other files import Config and then use it directly like:

import { Config } from '../shared/config/env.config';

export function main() {
    console.log(`Environment: ${Config.ENV}`);
}

I've been looking at something like this solution, but it seems I'd have to switch everything from using Config directly to using an injected ConfigService.

Do I need to switch from a constant to a service in order to support the run-time load? If not, how do I create the constant on the fly?

ricksmt
  • 888
  • 2
  • 13
  • 34
  • 1
    Have a look here. https://stackoverflow.com/a/47938411/1160794 You can keep using your Config variable, which will get initialised once from a service before the app loads – David Feb 13 '18 at 21:18
  • I'll also note that I needed this to avoid a circular dependency: [https://stackoverflow.com/a/47097841/1680677](https://stackoverflow.com/a/47097841/1680677). – ricksmt Feb 13 '18 at 23:12
  • Not if you use angular 5.2.3 or greater https://stackoverflow.com/a/48683766/1160794 – David Feb 14 '18 at 08:01

0 Answers0