简体   繁体   English

Angular如何针对不同环境共享json配置文件的相同设置

[英]Angular how to share the same setting of json configuration file for different environment

I have an Angular 9+ application that uses APP_INITILIZER to load the external json configuration file so that we can modify the file without rebuilding the app.我有一个 Angular 9+ 应用程序,它使用APP_INITILIZER加载外部 json 配置文件,这样我们就可以在不重新构建应用程序的情况下修改文件。

We have a few such configuration files for different environments like dev , qa , staging , etc. And in angular.json file, we specify which file to use in assets section.我们有一些这样的配置文件,用于不同的环境,如devqastaging等。在angular.json文件中,我们指定在assets部分使用哪个文件。

We have some settings that are not related to environment.我们有一些与环境无关的设置。 For example, the settings for colour例如,颜色的设置

...
,
{
   "title":"red",
   "header":"blue"
},
...

The issue is each time we add new settings like above, we need to duplicate it in every config file.问题是每次我们像上面一样添加新设置时,我们需要在每个配置文件中复制它。 I wonder if there's a way to only add the common settings once, and reuse them in all other config files?我想知道是否有办法只添加一次常用设置,然后在所有其他配置文件中重用它们?

The approach I usually take is to have a common environment file like environment.common.ts but the file looks slightly different我通常采用的方法是拥有一个通用环境文件,例如 environment.common.ts 但文件看起来略有不同

export const commonEnv = {
  title: 'red',
  header: 'blue'
};

I then just merge the settings with the normal environment file using Object.assign.然后我使用 Object.assign 将设置与正常环境文件合并。

import { commonEnv } from "./../environment.common";
import { environment } from './../environments/environment';

const mergedSettings = Object.assign(commonEnv, environment);

You can now use mergedSettings to get to all the settings.您现在可以使用 mergeSettings 来获取所有设置。

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

相关问题 Angular Js将不同的条目与相同类别的JSON文件结合在一起 - Angular Js combine different entries with the same category of JSON file 不同的 babel 环境配置 - Different babel environment configuration "有没有办法加载 .json 配置文件并自动替换环境变量?" - Is there a way to load a .json configuration file and automatically substitute environment variables? 具有相同控制器的角度两个不同指令共享范围 - Angular two different directives with same controller share scope 在Angularjs中如何加载相同的javascript文件,但为不同的环境传递不同的密钥(Test,beta,prod) - In Angularjs how do I load the same javascript file but pass different keys for different environment (Test, beta, prod) 通过package.json脚本在文件中设置多个环境变量 - Setting Multiple Environment Variables In File Through package.json Script 如何将环境配置注入不同的Angular模块? - How to inject environment config to different Angular Modules? 运行时Angular 2 Webpack环境配置 - Angular 2 Webpack environment configuration at runtime 在Angular中将环境变量设置为常量 - Setting environment variables as constants in Angular 在 JSON 文件上写入并在 Angular 中的组件上显示相同内容 - Write on JSON file and display the same on component in Angular
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM