项目的Angular 2文件结构

时间:2017-06-26 08:59:26

标签: angular

假设我的项目中有以下文件结构(取自文档):

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

那么,我应该把它放在哪里:

- 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 

我不确定是否应将其置于核心或共享。

此致

4 个答案:

答案 0 :(得分:3)

通常,应用范围内的服务应保留在core模块中。通过这种方式,您可以确保它们将是单例,并且您不会同时运行同一服务的多个实例。

所以

  • 我会在core模块
  • 中保留大部分应用范围内的服务
  • shared模块中的所有常见表示组件,指令,管道和模块
  • 所有部分特定元素(例如服务,组件......)在其功能模块

你应该在这里阅读,这是非常好的解释:

答案 1 :(得分:1)

这有点取决于您的项目规模,但我更喜欢按用例进行结构化。在站点范围内使用的内容将位于共享或特定于用例的文件夹中。 看起来有点重构项目是有道理的。

也许这对你有所帮助,它基于mgechev/angular-seed,这是大型项目的一个很好的起点:

./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

答案 2 :(得分:1)

在进行任何代码重组或模块转换之前,为了让您快速了解这一点,每个文件夹结构都可以使开发变得简单,并帮助开发人员轻松找到模块。

因此,与功能相关的声明需要进入功能文件夹本身,并且您需要将共享文件夹中的不同文件夹放入共享文件夹中

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

答案 3 :(得分:0)

Angular 2+风格指南正在进行中。例如,您可以采用以下方案,例如此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
相关问题