gpt4 book ai didi

具有不同 "profiles"的 Angular ng build

转载 作者:太空狗 更新时间:2023-10-29 17:43:18 26 4
gpt4 key购买 nike

在 Maven (Java) 中,有可能构建,例如,具有不同“配置文件”的 webapp war,“配置文件”表示例如要放入配置文件的 Web 服务的 URL。因此,“测试配置文件”将指示与“生产配置文件”不同的 URL。

是否有类似于 ng build 的配置文件的东西?

最佳答案

对于Angular 6+:

environments 文件夹中为每个配置文件创建一个文件:

environments/environment.ts
environments/environment.prod1.ts
environments/environment.prod2.ts

并且在每个文件中放入相应配置文件的参数:

export const environment = {
production: true,
serverUrl: "http://prod1.site.com"
};

您可以像这样访问组件/服务中的参数:

import {environment} from '../../environments/environment';

@Injectable()
export class SomeService {
SERVER_URL: string = environment.serverUrl;

并在 configurations 下的 angular.json 中添加新的配置文件环境:

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

最后在构建应用程序时选择配置文件:

ng build --configuration = prod1

关于具有不同 "profiles"的 Angular ng build,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45158095/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com