角文件夹结构和组件服务 [英] Angular folder structure and component services
问题描述
我已经阅读了许多有关Angular文件夹结构的文章.我仍然不清楚我们将组件服务放在何处.组件之间的共享服务置于共享之下.但是,仅由组件使用的服务又如何呢? 通常,我将所有组件逻辑放入服务中,并为组件保留仅与UI内容相关的代码. 哪个更好用:
I have read many articles about Angular folder structure. It is still not clear to me where do we put component services. Shared services between components are put under shared. But what about a service that is used only by a component? Usually I put all my component logic into a service and leave the component with code relevant only to UI stuff. Which one is better to use:
每个组件及其服务都放在同一个文件夹中
Each component and its service into the same folder
.
├── app.component.html
├── app.component.ts
├── app.module.ts
├── app-routing.module.ts
└── shop
├── clients
│ ├── clients.component.html
│ ├── clients.component.ts
│ ├── clients.component.css
│ └── clients.service.ts
├── orders
│ ├── orders.component.html
│ ├── orders.component.ts
│ ├── orders.component.css
│ └── orders.service.ts
├── shop.module.ts
└── shop-routing.module.ts
或服务文件夹下模块的所有服务
or all services of a module under a services folder
.
├── app.component.html
├── app.component.ts
├── app.module.ts
├── app-routing.module.ts
└── shop
├── clients
│ ├── clients.component.html
│ ├── clients.component.ts
│ └── clients.component.css
├── orders
│ ├── orders.component.html
│ ├── orders.component.ts
│ └── orders.component.css
├── services
│ ├── clients.service.ts
│ └── orders.service.ts
├── shop.module.ts
└── shop-routing.module.ts
推荐答案
这一切与 语义 和 可读性 我认为强".仅以适合您的方式进行操作,而您的方便并不会成为一种好的做法.对于新手或替代开发人员而言,应该很容易在短时间内找出您的方法,因此您应该专注于此.
It is all about Semantics and Readability in my opinion. Just doing it in a way that suits you and your convenience won't make it a good practice. It should be easy for a newbie or substitute developer to figure out your method in not time, so you should probably focus on that.
要记住的提示:
1)服务应该易于关联并且不会造成任何混乱
1) Services should be easily relatable and shouldn't create any confusion
2)路径导航不应变得复杂(例如:避免出现../../../../的情况)
2) Path navigation shouldn't become complex ( for eg: avoid cases of ../../../../ )
3)共享服务应放置在有意义的文件夹下
3) Shared services should be placed under a meaningful folder
我要说的是,您建议的第二种方法是遵循的通用标准.
I would say that the second method you suggested is the common standard followed.
引用: https://itnext.io/在角度d987de65ec7中选择高度可缩放的文件夹结构
这篇关于角文件夹结构和组件服务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!