简体   繁体   English

如何根据环境设置aurelia中的常量

[英]How to set a constant in aurelia by environment

I'd like my API's base URL to change from dev to prod . 我希望我的API的基本URL从dev变为prod In Angular I user to use a config.json file which was later injected into the app using grunt-env 在Angular I用户中使用config.json文件,该文件稍后使用grunt-env注入应用程序

If you use the Aurelia CLI , it will generate an environments directory inside of your aurelia_project . 如果您使用Aurelia CLI ,它将在aurelia_project内生成environments目录。

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. 在此目录中,您可以根据传递到au build/run命令的--env [dev/stage/prod]标志设置环境配置,这些配置将被复制到src目录中的--env [dev/stage/prod]

Then you can use import environment from './environment' to access your environment specific configuration values. 然后,您可以使用import environment from './environment'来访问特定于环境的配置值。

Another option that you can look into is the Aurelia Configuration Plugin , which also has dynamic environmental configs. 您可以看到的另一个选项是Aurelia配置插件 ,它还具有动态环境配置。

If you want to 'inject' it only once then what is stopping you from using a simple ES6 module ? 如果你只想“注入”一次那么什么阻止你使用简单的ES6模块? 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) 例如,你可以在config.js文件中使用类似的东西:(警告!我没试过运行它)

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 我已成功使用Aurelia-Configuration插件根据托管应用程序的域动态切换环境

More info https://github.com/Vheissu/Aurelia-Configuration/blob/master/README.md#get-started to do this 更多信息https://github.com/Vheissu/Aurelia-Configuration/blob/master/README.md#get-started这样做

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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