根据环境配置基本URL [英] Configure base URL depending on environment

查看:136
本文介绍了根据环境配置基本URL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我们有Angular 4+应用程序,该应用程序需要位于不同环境中的不同相对根URL中,即:

Suppose we have Angular 4+ app that needs to be located in different relative root URLs on different environments, i.e.:

  • http://localhost:4200/index.html用于开发
  • http://prod.server.com/angular-app/index.html用于生产
  • http://localhost:4200/index.html for development
  • http://prod.server.com/angular-app/index.html for production

最有可能,我们希望在我们的environment.x.ts文件中具有该选项:

Most likely, we'd want to have that option in our environment.x.ts files:

export const environment = {
    production: false,
    appRoot: "/"
};

export const environment = {
    production: true,
    appRoot: "/angular-app/"
};

我们如何配置Angular构建/运行时基础结构以根据environment.x.ts文件中的此选项自动调整应用程序?

How can we configure Angular build/runtime infrastructure to automatically adjust the app depending on this option in environment.x.ts files?

更新: 由于我是通过Visual Studio构建/发布系统间接使用Angular CLI工具链的( template ),最好有一个完全基于Angular CLI + *.json/*.ts/*.js文件的解决方案.这样,它将适用于可以使用Angular CLI的任何构建系统.

UPDATE: Since I'm using the Angular CLI toolchain indirectly via Visual Studio build/publish system (template), it'd be nice to have a solution based completely on Angular CLI + *.json/*.ts/*.js files. This way it would be suitable for any build system where Angular CLI could be used.

推荐答案

如果您使用的是Angular CLI,则可以执行以下操作:

If you are using the Angular CLI you can do:

ng build --prod --base-href /myUrl/

OR

ng build --prod --bh /myUrl/

这篇关于根据环境配置基本URL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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