根据环境配置基本URL [英] Configure base URL depending on environment
问题描述
假设我们有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 developmenthttp://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屋!