[英]Angular config according to environment (dev, staging, production)
It's silly, I know - every time I change environment I comment out and back my app's config factory. 我知道这很愚蠢-每次更改环境时,我都会注释掉并返回我的应用程序的配置工厂。 There must be a better way, is there? 一定有更好的方法吗?
I though of checking the URL but this seems like a bad practice. 我虽然检查了URL,但这似乎是一种不好的做法。 The other option I had in mind was using some sort of an gulp NPM during the build process - however development has no build process. 我想到的另一个选择是在构建过程中使用某种类型的NPM-但是开发没有构建过程。
angular.module('myApp.services')
.factory('GemsConfig', function () {
return {
//apiServer: "http://localhost:3000/v2/",
//apiServer: "https://staging.gems.org/v2/",
apiServer: "https://api.gems.org/v2/",
//giphyServer: "http://api.giphy.com/v1/gifs/",
giphyServer: "https://api.giphy.com/v1/gifs/",
TTL: 5000,
OS: 'web',
version: '1.1',
apiVer: '2.0',
checkBalanceEveryXSeconds: 600,
//giphyKey: 'xxx', // testing
giphyKey: 'xxxx'
};
})
Create two config one for production and one for development. 创建两个配置,一个用于生产,一个用于开发。 And create one variable which can have two values either "production" or "development". 并创建一个变量,该变量可以具有“生产”或“发展”两个值。 Based on this variable call your config. 基于此变量调用您的配置。 By this you need to change only your variable's name. 这样,您只需要更改变量的名称即可。
Eg. 例如。
angular.module('myApp.services')
.factory('GemsConfig', function () {
var currentEnv = "production";
var config = {
development :{
apiServer: "https://api.gems.org/v2/",
giphyServer: "https://api.giphy.com/v1/gifs/",
TTL: 5000,
OS: 'web',
version: '1.1',
apiVer: '2.0',
checkBalanceEveryXSeconds: 600,
giphyKey: 'xxxx'
},
production :{
apiServer: "https://api.gems.org/v2/",
giphyServer: "https://api.giphy.com/v1/gifs/",
TTL: 5000,
OS: 'web',
version: '1.1',
apiVer: '2.0',
checkBalanceEveryXSeconds: 600,
giphyKey: 'xxxx'
}
};
return config[currentEnv];
})
EDIT 编辑
Or better solution: Create three config 1) common 2) production 3) development 或更佳的解决方案:创建三个配置1)通用2)生产3)开发
You can keep your common configuration params inside 'common' common config,or you can override them either in 'production' or 'development'. 您可以将通用配置参数保留在“通用”通用配置中,也可以在“生产”或“开发”中覆盖它们。 And at least you have to merge your "current environment" - (production or development) config and "common" config. 并且至少您必须合并 “当前环境”-(生产或开发)配置和“通用”配置。 For this you can use angular.extend function. 为此,您可以使用angular.extend函数。
EG. 例如。
angular.module('myApp.services')
.factory('GemsConfig', function () {
var currentEnv = "production";
var config = {
common:{
TTL: 5000,
OS: 'web',
version: '1.1',
apiVer: '2.0',
checkBalanceEveryXSeconds: 600,
giphyKey: 'xxxxx'
},
development :{
apiServer: "https://api.gems.org/v2/",
giphyServer: "https://api.giphy.com/v1/gifs/",
},
production :{
apiServer: "https://api.gems.org/v2/",
giphyServer: "https://api.giphy.com/v1/gifs/",
TTL:800,
}
};
return angular.extend(config['common'],config[currentEnv]);
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.