Angular 2 architecture - place for shared services modulo

I've read about some Angular 2 project architecture, but this is not my case.

My question is where to put the generic service that is used across all modules.

My project has several modules, for example: admin module

and user module

.

This is this structure:

app |admin | |- some admin components | |- admin.module |user | |- some user components | |- shared | |- user.service | |- user.module

The problem is this: I want to get user information in some components of the admin module. So I import the custom service into the admin module and host it in the admin module provider.

I think this is not a good practice.

I found this: http://jasonwatmore.com/post/2016/08/16/angular-2-jwt-authentication-example-tutorial But I don't know if it's good practice. Should I post user.service

up app/_services

for general use? Should I create a service.module to initialize user.service, then import the service module into another module?

app |_services | |- user.service |admin | |- some admin components | |- admin.module - import and set user.service to provider |user | |- some user components | |- user.module - import and set user.service to provider

`

+3


source to share


1 answer


It's fine to import custom service into admin components if you need to. It simply means that the administration module will depend on a custom module, which is fine, but you might want to put some effort into structuring your dependency trees so they don't get out of hand.

Keep all custom services and custom components inside the user, don't put the custom service in the shared services folder. It is the most scalable architecture. This is called the folder by function or single responsibility. If you run a "shared" folder where you simply log into services from all of your modules, this folder will soon be huge and all parts of your application will depend on this folder, tightly linking your entire application.



See the official John Papa angular2 style guide which talks about one responsibility: https://angular.io/docs/ts/latest/guide/style-guide.html

+4


source







All Articles