Run ng build with custom configurations

可紊 提交于 2019-12-24 00:25:29

问题


I have Angular project which I would like to deploy on Apache server. I use ng build but I would like to custom address and endpoint for backend.

proxy.conf.json:

{
  "/api/*": {
    "target": "http://localhost:8080",
    "secure": false,
    "logLevel": "debug",
    "changeOrigin": true
  }
}

This configuration is not applied at all. How I can set it properly in order to change configurations?

Environment ts file:

import {environment as prod} from './environment.prod';

export const environment = Object.assign(prod, {
  production: false
});

回答1:


Assuming you are using Angular (>v6), and you have created multiple environment files as per requirements.

So what you need to do is, go to angular.json file

angular.json > projects > projectName > architect > build > configurations > fileReplacements

and here you need to replace files name with your files name like this -

"replace": "src/environments/environment.ts",
"with": "src/environments/environment.live.ts"



回答2:


You can define different environment files. Below example for "dev":

export const environment = {
    production: false,
    envName: 'dev',
    configPath: './assets/config/config.dev.json'
    ...
};

Add a configuration part for "dev" in "angular.json" file, like that:

"dev": {
  "fileReplacements": [
    {
      "replace": "src/environments/environment.ts",
      "with": "src/environments/environment.dev.ts"
    }
  ],
  ....

And user this command to build : ng build --configuration=dev Take a look at this post : How to set environment via `ng serve` in angular 6



来源:https://stackoverflow.com/questions/53411274/run-ng-build-with-custom-configurations

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!