我想定义一些 application settings ,但我想根据我是否在“调试”模式下运行(例如 gruntserve
)或最终编译的应用程序是否正在运行来提供不同的值(例如 grunt
的输出)。也就是说,类似于:
angular.module('myApp').factory('AppSettings', function() {
if (DebugMode()) { // ??
return { apiPort: 12345 };
} else {
return { apiPort: 8008 };
}
});
我怎样才能做到这一点?
最佳答案
我在应用程序中处理它的方式:
- 将一个环境的所有配置数据移至一个文件:config.js、config.json...您的应用认为易于阅读的任何内容。
- 现在修改您的配置文件,使用 grunt 配置值将其转换为模板,并使用
grunt-template
生成文件。作为构建的一部分 - 例如:app.constant('myAppConfig', {bananaHammocks: <%= banana.hammocks %>});
- 最后,添加
grunt-stage
根据环境切换 grunt 配置值:创建不同的config/secret/(env).json
文件,更新您的模板 (app.constant('myAppConfig', {bananaHammocks: <%= stg.banana.hammocks %>});
),然后grunt stage:local:build
或grunt stage:prod:build
我发现这是复杂性和功能之间的良好平衡(环境之间的分离,运行时代码不关心构建选项,...)
关于javascript - debug/local ("grunt serve") 与 dist/build ("grunt") 的不同设置?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33003129/