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.
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
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.
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.