[英]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.