How to set a constant in aurelia by environment
Asked Answered
A

3

12

I'd like my API's base URL to change from dev to prod. In Angular I user to use a config.json file which was later injected into the app using grunt-env

Abstruse answered 18/4, 2015 at 21:2 Comment(4)
This is a pretty open question because it all depends on how you are serving and could definitely be up to personal choice. I don't see anything wrong with putting it in a json fileUndertint
I wouldn't want to read that json file over and over again - it would make more sense to inject it once (somewhere) in the configAbstruse
Yeah that is what I meant just use the http client to read it one time should be super simple.Undertint
Thanks PW Kad, any chance you have a gist with an example of how to do it in practice?Abstruse
A
22

If you use the Aurelia CLI, it will generate an environments directory inside of your aurelia_project.

Within this directory you can setup environmental configs that will be copied into environment.js in your src directory based the --env [dev/stage/prod] flag that you pass into your au build/run commands.

Then you can use import environment from './environment' to access your environment specific configuration values.

Another option that you can look into is the Aurelia Configuration Plugin, which also has dynamic environmental configs.

Allowable answered 29/7, 2016 at 21:28 Comment(0)
R
1

If you want to 'inject' it only once then what is stopping you from using a simple ES6 module ? It should be loaded only once from the server.

For instance you could something like that in a config.js file : (warning ! I didn't try to run it)

export var Config = {
    path : 'path to find'
};

you can then use your module anywhere you need it :

import {Config} from 'config';
Rufina answered 26/4, 2015 at 13:33 Comment(1)
Yes, injected once - but different values when running locally, and when running on production. In Angular + grunt, I used grunt serve to be DEV constants, and grunt build to be production constants.Abstruse
E
0

I have successfully used the Aurelia-Configuration plugin to dynamically switch environments based on the domain that hosts the app

More info https://github.com/Vheissu/Aurelia-Configuration/blob/master/README.md#get-started to do this

Ejaculate answered 6/11, 2018 at 12:50 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.