Where to save global settings in Angular 4

后端 未结 4 2255
被撕碎了的回忆
被撕碎了的回忆 2021-02-19 05:45

In Angular (Type Script) there are many config files. Which is the right one to save a global setting?

For example, when I am calling API from local then my rootUr

4条回答
  •  天命终不由人
    2021-02-19 06:25

    I have another way to define global settings. Because if we defined in ts file, if build in production mode it is not easy to find constants to change value.

    export class SettingService  {
    
      constructor(private http: HttpClient) {
    
      }
    
      public getJSON(file): Observable {
          return this.http.get("./assets/configs/" + file + ".json");
      }
      public getSetting(){
          // use setting here
      }
    }
    

    In app folder, i add folder configs/setting.json

    Content in setting.json

    {
        "baseUrl": "http://localhost:52555"
    }
    

    In app module add APP_INITIALIZER

     {
          provide: APP_INITIALIZER,
          useFactory: (setting: SettingService) => function() {return setting.getSetting()},
          deps: [SettingService],
          multi: true
        }
    

    with this way, I can change value in json file easier.

    I applied this in project for baseUrl, dateformat, sessiontimeout...

提交回复
热议问题