如何在 Angular 2 应用程序中配置不同的开发环境 [英] How to config different development environment in Angular 2 app

查看:22
本文介绍了如何在 Angular 2 应用程序中配置不同的开发环境的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个常量文件

export class constants {
    public static get API_ENDPOINT(): string { return 'https://dvelopment-server/'; }
}

然后我将它导入到我的服务中

And I imported it to my service

private _registrationUrl = constants.API_ENDPOINT+'api/v1/register';

如何通过服务器更改更改端点.我有开发服务器登台服务器和本地服务器.我希望应用程序检测环境变化.

How can I change the endpont with server change . I have development server staging server and local server. I want app to detect the environment change.

在我的 angular 1 应用程序中,我为此使用了 envserviceprovider.我可以在 angular 2 应用中使用相同的吗?

In my angular 1 app I used envserviceprovider for this. Can I use the same in angular 2 app ?

推荐答案

我已经通过添加类方法解决了这个问题

I have solved the issue by adding a class method

export class config {

    public static getEnvironmentVariable(value) {
        var environment:string;
        var data = {};
        environment = window.location.hostname;
        switch (environment) {
            case'localhost':
                data = {
                    endPoint: 'https://dev.xxxxx.com/'
                };
                break;
             case 'uat.server.com':
                data = {
                    endPoint: 'https://uat.xxxxx.com/'
                };
                break;

            default:
                data = {
                    endPoint: 'https://dev.xxxx.com/'
                };
        }
        return data[value];
    }
}

为我服务

private _loginUrl = config.getEnvironmentVariable('endPoint')+'api/v1/login;

这篇关于如何在 Angular 2 应用程序中配置不同的开发环境的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆