我可以根据上述场景将可重用组件的服务放置在 Angular 的核心模块中吗

问题描述 投票:0回答:1

├── src
│ ├── 应用程序
│ │ ├── 核心
│ │ │ ├──服务
│ │ │ ├── core.module.ts
│ │ ├── 共享
│ │ │ ├── 产品形态
│ │ │ │ ├──product-form.component.ts|html|css
│ │ │ │ ├── 产品表单.service.ts
│ │ │ │
我有 2 个模块 coreshared。我在 shared 模块中有 product-form 组件。而且 product-formproduct-form.service。我使用该服务来格式化表单数据并将其再次返回到 product-form.component。该服务仅由 product-form.
使用,所以我需要知道,我可以将 product-form.service 放在 product-form 组件中,如上面的结构吗?或者我应该将它放在 core 模块服务文件夹中?

我是 Angular 的初学者。

谢谢你。

angular typescript directory-structure file-structure
1个回答
0
投票

是的,可以。设计上很少有接触:

  1. 将所有服务分开。
  2. 在组件范围内保持专注服务。
src
 |-app
 |  |-core
 |     |-services
 |-shared
 |  |-components
 |  |  |-my-component
 |  |    |-my-component.component.html
 |  |    |-my-component.compoennt.ts
 |  |    |-my-component.service.ts

但是如果你问我该怎么做,我将创建一个库并将我所有共享的服务和组件放在那里。在这种情况下,它们将在库中单独烘焙,主应用程序将仅调用所需的内容。

projects
 |-my-library
 |  |-src
 |  |  |-lib
 |  |     |-components
 |  |     | |-my-component.component.html
 |  |     | |-my-component.component.ts
 |  |     |-services
 |  |     | |-my-component.service.ts
src
 |-app
 |  |-core
 |  |  |-services
 | ...

如果您想了解有关库的更多信息:https://angular.io/guide/creating-libraries

© www.soinside.com 2019 - 2024. All rights reserved.