[英]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.我们有一些这样的配置文件,用于不同的环境,如
dev
、 qa
、 staging
等。在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.