简体   繁体   English

如何重用Angular项目的构建

[英]How to reuse the build for an Angular project

How can I reuse my Angular builds so I do not have to build for every specific environment? 如何重用我的Angular构建,这样我就不必为每个特定环境构建?

we need to find a way to manipulate the environments in runtime in Angular! 我们需要找到一种在Angular中运行时操作环境的方法!

We have settings for each environment and we use NG build --env=dev and build for the dev environment. 我们为每个环境设置了设置,我们使用NG build --env = dev并为开发环境构建。 How can I change the configuration in QA, UAT and production environments? 如何更改QA,UAT和生产环境中的配置?

Toolset: .Net Visual Studio Team Services, Angular 2 工具集:.Net Visual Studio Team Services,Angular 2

Is there no way to do this at runtime? 在运行时没有办法做到这一点吗? Are we stuck with the build time/design time? 我们是否坚持构建时间/设计时间?

Also can we consider selecting environments based on our urls which has a postfix ? 我们还可以考虑根据我们的网址选择具有后缀的环境吗? https://company-fancyspawebsite- qa .azurewebsites.net https:// company-fancyspawebsite- qa .azurewebsites.net

PS:We are using the Angular 2 environments folder and app settings files for each environment. PS:我们正在为每个环境使用Angular 2环境文件夹和应用程序设置文件。

在此输入图像描述

在此输入图像描述

在此输入图像描述

I use a config service to supply editable config settings at run-time. 我使用配置服务在运行时提供可编辑的配置设置。 (this is using angular-cli) (这是使用angular-cli)

config.service.ts config.service.ts

import { Injectable } from '@angular/core';
import { Http, Headers, RequestOptions, Response } from '@angular/http';

export interface Config {
    PageSize: number;
    EnableConsoleLogging: boolean;
    WebApiBaseUrl: string;
}

@Injectable()
export class ConfigService {
    private config: Config;

    constructor(private http: Http) { }

    public getConfigSettings(): Config {
        if (!this.config) {
            var Httpreq = new XMLHttpRequest();
            Httpreq.open("GET", 'config.json', false);
            Httpreq.send(null);

            this.config = JSON.parse(Httpreq.responseText);

            if (this.config.EnableConsoleLogging)
                console.log("Config loaded", this.config);
        }

        return this.config;
    }
}

config.json located in my src folder config.json位于我的src文件夹中

{
  "WebApiBaseUrl": "http://myWebApi.com",
  "EnableConsoleLogging": true,
  "PageSize": 10
}

add config.json to your assets in .angular-cli.json 将config.json添加到.angular-cli.json中的资源

{
  },
  "apps": [
    {
      "assets": [
        "config.json"
      ]
    }
  }
}

how to use it 如何使用它

export class MyComponent {
    private config: Config;

    constructor(private configService: ConfigService) {
        this.config = configService.getConfigSettings();

        console.log(this.config.WebApiBaseUrl);
    }
}

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

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