Posted on Updated on - Arjun

Angular – Global API service request method

Using HTTP service directly in all over the application is won’t really scale, its only work for small applications. Having 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 use it inside my change password service(changepassword.service.ts), the code should be something like shown below

I hope you like this Post, Please feel free to comment below, your suggestion and problems if you face - we are here to solve your problems.

Arjun
I am Arjun from Hyderabad (India). I have been working as a software engineer from last 7+ years, and its my passion to learn new things and implement them as a practice. Aside from work, I likes gardening and spending time with pets.
DMCA.com Protection Status