Angular 4 create assets / files per environment?

前端 未结 1 1050
你的背包
你的背包 2021-01-14 23:23

We use environment variables within our angular app to read settings etc but is there a way to generate assets/files on build?

Basically we\'d like to create an \'au

相关标签:
1条回答
  • 2021-01-14 23:44

    What we are doing in our case is actually having an config.json and config.[env-name].json files in app/config folder that configured in project assets. The config.json file is getting fetched before angular bootstrap using browser Fetch API

    On our build server we are just replacing the content of config.json withconfig.staging.json or config.prod.json based on environment build. Also we have AppSettings class that gets created on bootstrap. Here is how it is looks like:

    fetch(configUrl, { method: 'get' })
    .then((response) => {
      response.json()
        .then((data: any) => {
          if (environment.production) {
            enableProdMode();
          };
          platformBrowserDynamic([{ provide: AppSettings, useValue: new AppSettings(data.config) }]).bootstrapModule(AppModule);
        });
    });
    

    UPDATE: If you need to stick some values based on your env in to index.html you might need to consider doing that on your build server. You can rather string replace the values or you can have index.[env-name].thml files so you just overwrite the index.html based on environment build.

    Also check out this issues
    - https://github.com/angular/angular-cli/issues/7506
    - https://github.com/angular/angular-cli/issues/3855

    0 讨论(0)
提交回复
热议问题