Access environment variables in production build Angular 4

后端 未结 4 837
萌比男神i
萌比男神i 2020-11-28 15:07

I want to deploy a production build of angular app with a configurable api url for the user to test it out. I use the environment.ts but after the production build, I do not

4条回答
  •  轻奢々
    轻奢々 (楼主)
    2020-11-28 15:24

    The environment*.ts file contain build time configurations, which you cannot change after the build. If you need to change your configuration after the build, you need to put them in a different place and retrieve them dynamically when the application starts

    What you can do is:

    Step #1: put your json configuration files under src/assets/config/[envName].json.

    Note: it has to be json format, not ts format

    Step #2: Add a new config service

    import {Inject, Injectable} from '@angular/core';
    import {HttpClient} from "@angular/common/http";
    import {Observable} from 'rxjs/Rx';
    import {environment} from "../../environments/environment";
    
    /**
     * Declaration of config class
     */
    export class AppConfig
    {
    //Your properties here
      readonly production: boolean;
      readonly name: string;
    
      readonly apiBaseUrl: string;
    
    }
    
    /**
     * Global variable containing actual config to use. Initialised via ajax call
     */
    export let APP_CONFIG: AppConfig;
    
    /**
     * Service in charge of dynamically initialising configuration
     */
    @Injectable()
    export class AppConfigService
    {
    
      constructor(private http: HttpClient)
      {
      }
    
      public load()
      {
        return new Promise((resolve, reject) => {
    
          let confName = environment.name + '.json';
          this.http.get('/assets/config/' + confName).map(res => res as any).catch((error: any): any => {
            reject(true);
            return Observable.throw('Server error');
          }).subscribe((envResponse :any) => {
            let t = new AppConfig();
            //Modify envResponse here if needed (e.g. to ajust parameters for https,...)
            APP_CONFIG = Object.assign(t, envResponse);
            resolve(true);
          });
    
        });
      }
    }
    

    Step #3: In your main module, add this before declaring the module

    /**
    * Exported function so that it works with AOT
    * @param {AppConfigService} configService
    * @returns {Function}
    */
    export function loadConfigService(configService: AppConfigService): Function 
    
    {
      return () => { return configService.load() }; 
    }
    

    Step #4: Modify the module providers to add this providers: [ …

      AppConfigService,
      { provide: APP_INITIALIZER, useFactory: loadConfigService , deps: [AppConfigService], multi: true },
    
    
    ],
    

    Step 5: In your code, instead of using environment.configXXX, use this

    import {APP_CONFIG} from "../services/app-config.service";
    
    //…
    return APP_CONFIG.configXXX;
    

    This is a simplified example, you'll actually need to make some changes if you use angular universal as you need to have absolute urls when making an http call

提交回复
热议问题