2

Let say I have file structure like below in my project (taken from documentation):

-app
--core
--feature1
--feature2
--shared

So, where should I put something like:

- communicates.service (service to manage communicates (.message(), .error())
- communicates.component (component which is responsible for content of communicates on my page)
- authorization.service
- and for example feature1.service (feature2 uses this service)
- feature2.service 

I am not sure if I should put it to core or shared.

Regards

1
  • I normally just created a "services" directory and stick them in there under the "app" Commented Jun 26, 2017 at 9:00

4 Answers 4

3

Usually app-wide services should be kept in the core module. This way you'll be sure that they will be singletons and you won't have multiple instances of the same service running at the same time.

So

  • I would keep most of the app-wide services in the core module
  • All common presentational components, directives, pipes, modules in the shared module
  • All section specific elements (eg. services, components...) in it's feature module

You should have a read here, it's very well explained:

Sign up to request clarification or add additional context in comments.

8 Comments

Yes, I know it, but for example I have features: task and task-list. Task list should be "shared" for other features which can display task list. But also task-list can be as subfolder in task feature folder. How to solve this case?
I'm sorry I didn't quite get your question... if both task and task-list are shared features, why don't you add them in the shared module?
task is not a shared feature. Task.component feature is a subpage but task-list.component may be used in other features.
So, as I understood: task-list.component is shared and it uses internally task.component. In this case I would put both of them in the shared.module but I'll just export only task-list.component since it's the only one used other features.
If some components use each other should they both be in shared?
|
1

It a bit depends on your project size, but I prefer structuring by use case. And stuff that is used site-wide will be in a shared or in a use-case specific folder. Looks like it makes sense to refactor the project a bit.

Maybe this helps you a bit, it is based on mgechev/angular-seed, which is a great starting point for larger size projects:

./auth
./auth/store
./auth/store/actions
./auth/store/actions/action-creators
./auth/store/async-services
./auth/store/facades
./auth/store/reducers
./data-models
./data-models/classes
./data-models/interfaces
./data-models/types
./root-store
./root-store/async-services
./root-store/facades
./root-store/store
./shared
./shared/accordion
./shared/config
./shared/disclaimer
./shared/footer
./shared/helpers
./shared/loading-spinner
./shared/navbar
./shared/pipes
./storefront
./storefront/breadcrumbs
./storefront/error
./storefront/facets
./storefront/hero-banner
./storefront/product-catalog
./storefront/product-catalog/product-list
./storefront/product-catalog/product-list/product-card
./storefront/product-detail
./storefront/product-detail/aside
./storefront/product-detail/assets
./storefront/product-detail/features
./storefront/product-detail/header
./storefront/product-detail/media
./storefront/search
./storefront/store
./storefront/store/actions
./storefront/store/actions/action-creators
./storefront/store/async-services
./storefront/store/facades
./storefront/store/reducers

Comments

1

Before you do any code restructuring or shifting of modules, just to give you quick thought about this, every folder structuring is done to make the development easy and help developers to easily locate the modules.

so the feature related declaration need to go in feature folder itself and something which you makes out as common need to go in the shared folder with different folders in it

-app
--feature1
---feature1.module
---feature1.service
---feature1.model
--feature2
---feature2.module
---feature2.service
---feature2.model
--shared
---communicates
----communicates.service
----communicates.component

3 Comments

Yes, I know it, but for example I have features: task and task-list. Task list should be "shared" for other features which can display task list. But also task-list can be as subfolder in task feature folder. How to solve this case?
not sure you expressed this correclty, but as per your comment you have already said that task-list need to be shared for other features, so this in any case need to go into shared folder
task.component in features uses task-list.component and task-editor.component. I have similar model also for other features, for example suites.component with suites-list.component and suites-editor.component. suites-editor.component may use task.list.component. There are many similar dependencies
0

Angular 2+ Style Guide is a work in progress. For example, you can take the following scenario like describe in this repository.

-app
--components
---components.module.ts
---feature1
---communicate
----communicate.component.|html|css|spec.ts|ts
--guards
--models
--pipes
--services
---feature1.service.ts
---feature2.service.ts
---communicate.service.ts
---authorization.service.ts

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.