简体   繁体   中英

How to set a constant in aurelia by environment

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

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.

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';

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM