根据环境配置基本 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 工具链(模板),如果有一个完全基于 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/
或
ng build --prod --bh /myUrl/
这篇关于根据环境配置基本 URL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!