Where to save global settings in Angular 4

后端 未结 4 2268
被撕碎了的回忆
被撕碎了的回忆 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:19

    This answer is similar to @trichetriche, with few more details on the code.

    For development/testing purpose

    environment.ts

    export const environment = {
      production: false,
      appUrl: 'localhost:4200'
    };
    

    For production

    environment.prod.ts

    export const environment = {
          production: true,
          appUrl: 'mywebsite.com'
        };
    

    Usage

    service.ts

    import { environment } from '../../environments/environment';
    
    this._http.get(environment.appUrl, requestHeaders(options));
    

    Make a note of production parameter in all the environment files. I believe you could also create more environments like environment.unittesting.ts.

提交回复
热议问题