Angular 2

Angular – Global API service request method

Using HTTP service directly in all over the application is not scalable, its only work for small applications. Having a central location to manage HTTP calls, will reduce the maintenance and its robust solution. We can inject this global service into any component.

centralized location for all api calls

Global API service request method

Lets create a new service called APIService, filename name is api.service.ts, you can place this wherever you want in your application, preferred location is app\shared. As I have copied directly from my project adjust your env config and rxjs dependencies as for your application setup.

Below is my complete working common api service request method.

Now add this to app module providers array

How to use common service method

Just like any other class, import APIService into current locations, for example, I want to use it inside my change password service(changepassword.service.ts), the code should be something like shown below

By Arjun

I am Arjun from Hyderabad (India). I have been working as a software engineer from the last 7+ years, and it is my passion to learn new things and implement them as a practice. Aside from work, I like gardening and spending time with pets.

1 reply on “Angular – Global API service request method”

Leave a Reply

Your email address will not be published. Required fields are marked *