简体   繁体   English

根据环境(开发,登台,生产)进行角度配置

[英]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.

相关问题 在生产环境中以DEV模式运行 - Vue in DEV mode in production environment 在 React 应用程序中检查 HTML 文件中的生产和暂存环境 - Check Production and Staging environment inside HTML file in a React App Heroku nuxt生产部署正在使用暂存配置变量 - Heroku nuxt production deployment is using staging config vars 我的页面在开发环境中不可滚动,但在生产环境中可滚动 - My page is not scrollable on the dev environment, but scrollable on the production 在开发和生产环境中获取基本URL - Get base url in dev and production environment angular2-starter配置文件(带有环境(测试,生产)变量,也称为.env或.conf) - angular2-starter config file (with environment (testing, production) variables aka .env or .conf) 如何在生产环境中的 Reactjs 中配置 COR - How to config CORs in Reactjs on Production environment 在创建暂存/生产构建(Angular)时使用本地参考中断 - Using Local reference breaks while create a staging/production build (Angular) 导出到 .xls 适用于开发环境但不适用于生产/失败 - 网络错误 - Export to .xls works on dev environment but not on production / Failed - Network Error CSS 关键帧在开发环境中运行良好但在生产环境中运行不正常 - CSS keyframes working fine in dev environment but not on production build
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM