简体   繁体   中英

Angular style guide, where to put component services

I have a modal component. The style guide states that we should move complex logic into services for components: https://angular.io/guide/styleguide#delegate-complex-component-logic-to-services

It also states that all singleton services should be put into a core feature. So, where do I put my component service?

I want to share a function between my component and anything that is using it. So, if I create a modal.service.ts, should it go in the /shared/modal folder along with the modal.component.html/css/spec/ts ??

It's more of a question that involves personal opinions. Generally speaking, with the new provider configuration syntax providedIn: 'root' released with Angular 6, putting your services inside CoreModule is getting fewer benefits than it used to previously.

If your service is a companion with the component it provides, I would suggest you create an independent feature module for it. In your case, DialogModule would be the way to go. Putting it inside either CoreModule or SharedModule is also fine but the only way I would strongly against is to put your dialog service inside Core, and your dialog component inside Shared because it may cause you trouble for future maintainance and readibility.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM