简体   繁体   English

使用 JSON 文件而不是环境文件仅用于 angular 9 中的构建应用程序

[英]Use JSON file instead of environment files only for build application in angular 9

I have Multiple json files like config.dv.json, config.local.json etc, and I need to load these json files based on environments by using single command as ng build --prod command.我有多个 json 文件,如 config.dv.json、config.local.json 等,我需要使用单个命令作为 ng build --prod 命令,根据环境加载这些 json 文件。

The only thing Angular CLI does is, it replaces environment.ts with the correct environment (eg environment.prod.ts). Angular CLI 唯一做的就是用正确的环境(例如 environment.prod.ts)替换 environment.ts。

You could add your config JSON files to your assets in a folder "configs" and name them environment-specific, as stated in your question ("dv.json", "local.json", etc.).您可以将配置 JSON 文件添加到文件夹“configs”中的资产中,并将它们命名为特定于环境的,如您的问题中所述(“dv.json”、“local.json”等)。 Now, in environment.ts you can reference your config like this, for example:现在,在 environment.ts 中,您可以像这样引用您的配置,例如:

export const environment = {
  name: "dev",
  config: "dv"
};

Finally, you just need to load your config file based on the environment, eg like this:最后,您只需要根据环境加载您的配置文件,例如:

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

this.http.get('./configs/' + environment.config + '.json').pipe(
map((res:any) => {
  let configJson = res.json();
})).subscribe();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在angular应用程序中从资产文件夹而不是environment.ts文件访问环境变量 - How to access environment variables from assets folder instead of environment.ts file in angular application Angular 构建命令没有替换环境文件 - Angular build command is not replacing environment files 使用 docker-build.yaml 环境变量在 angular package.Z466DEEC76ECDF65FCA6D3857DFZ - Use docker-build.yaml environment variable in angular package.json 角环境文件未通过ng serve / build复制 - Angular environment file not copied by ng serve/build Angular 构建时不替换环境文件 - Angular not replacing environment file when build Angular 项目 - Azure DevOps - 3 个环境文件。 在 yaml 文件中,我将指定要使用哪个环境文件的变量放在哪里? - Angular project - Azure DevOps - 3 Environment files. In the yaml file, where do I put the variable that designates which environment file to use? 如何更换 angular.json 上的环境文件 - how to replace environment file on angular.json 如何在Angular2应用程序中将.env文件与环境变量一起使用? - How can I do to use .env file with environment variables in Angular2 application? Angular中如何根据环境替换文件,编译前? - How to replace files according to environment in Angular, before build? 如何添加/设置环境 Angular 6 angular.json 文件 - How to add/set environment Angular 6 angular.json file
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM