dvl Batdelger
dvl Batdelger

Reputation: 151

Good or bad to provide services per component angular 4?

Actually, I have two questions about angular 4 best practice.

  1. I have a service named RemoteService, which is responsible for XHR calls. I want to catch component instance, which is using that service, in RemoteService. So I guess, it is better to provide that service per component level. Because I don't want every methods of the service take component instance by parameter. But, is it good or bad to provide service per component?. By the way: RemoteService will be used in hundreds of components.
  2. I'm trying to follow style guide of angular. Where should I place RemoteService?. Shared feature module or Core feature module?. It's purpose seems like ExceptionService and LoggerService. But it is not singleton service as I explained above. It may depends on answer of question 1.

Upvotes: 0

Views: 2436

Answers (1)

itamar
itamar

Reputation: 3967

So to answer both your questions - a RemoteService is definitely a good idea. I usually call mine ApiService.

This is a good idea for several reasons -

  • All http calls out of one file allows for a single endpoint for your external requests. It's more organized than having the calls come out of individual component-level services
  • You have more control over the calls. Meaning, if you needed to put some logic between the call from your component, and the actual http call (example - authentication check) - you don't want to run around your app changing it everywhere - instead you have a local function that wraps the http request, and provides the extra logic.

In terms of where to put it - John Papa's style guide suggests putting this file in the shared folder: https://johnpapa.net/angular-2-styles/

Upvotes: 2

Related Questions