How to store the root url of api in angular 4 app?

后端 未结 7 1472
Happy的楠姐
Happy的楠姐 2020-12-29 03:16

I followed the official tutorials and made services for the Apis but absolute url of the Api is hardcoded in services.

I want to keep the base url of Api somewhere s

7条回答
  •  一生所求
    2020-12-29 03:59

    Usually I put these in the const environment file. If you are using angular-cli, this is already provided for you if not you can create your own:

    export const environment = {  
      production: false,
      api: 'http://localhost:4200/api/'
    };
    

    You can have multiple environment file, like environment.production.ts, then with angular-cli you would run:

    ng build --environment=production
    

    If you are not using angular-cli, pretty sure you can build your own something similar.

提交回复
热议问题