1

我开始开发一个新的Angular 2应用程序,我试图遵循官方的Angular 2样式指南。我的应用程序大部分都是由一系列模块组成的(大约10个模块),它们对彼此一无所知。因此,我可以使用延迟加载并通过Angular 2路由器访问这些模块。几乎所有组件都共享时,与角度2目录结构混淆

|- app 
| |- +module1 
| | |- module1-list 
| | | |- module1-list.component.html 
| | | |- module1-list.component.ts 
| | |- module1-view 
| | | |- module1-view.component.html 
| | | |- module1-view.component.ts 
| | |- shared 
| | | |- module1.model.ts 
| | | |- module1.service.ts 
| | |- module1-container.component.html 
| | |- module1-container.component.ts 
| |- +module2 
| | |- <same structure with module1> 

在上述图中,moduleX-container.component组件是页面路由器点。 moduleX的所有其他组件都附加在它上面。

但是,有一些应用程序模块foobar需要使用所有其他模块的组件。根据官方的Angular 2风格指南,共享组件必须放在shared文件夹下。其结果是我的目录结构变成这样:

|- app 
| |- +foo (this makes use of all the components under /app/shared/) 
| | |- foo-container.component.html 
| | |- foo-container.component.ts 
| |- +bar (this makes use of all the components under /app/shared/) 
| | |- bar-container.component.html 
| | |- bar-container.component.ts 
| |- +module1 
| | |- module1-container.component.html 
| | |- module1-container.component.ts 
| |- +module2 
| | |- module2-container.component.html 
| | |- module2-container.component.ts 
| |- shared 
| | |- module1 
| | | |- module1-list 
| | | | |- module1-list.component.html 
| | | | |- module1-list.component.ts 
| | | |- module1-view 
| | | | |- module1-view.component.html 
| | | | |- module1-view.component.ts 
| | | |- shared 
| | | | |- module1.model.ts 
| | | | |- module1.service.ts 
| | |- module2 
| | | |- <same structure with module1> 

正如你可以看到,现在几乎所有的代码都放在shared文件夹下。因此,应用程序文件夹中的延迟加载组件(以+开头的那些组件)将仅保存访问共享组件的页面的线框。这是一个好的结构吗?它看起来与官方的Angular 2风格指南兼容,但同时我的代码有95%的代码在shared文件夹下,感觉有点奇怪。

EDIT 1:

另外,我想module1-view.componentmodule1-list.component由被加载(或下嵌套)懒惰和非延迟加载组件。因此,在我的第一个目录结构方法中,在延迟加载的父组件(+module1)下有module1-view.componentmodule1-list.component似乎是错误的。

您认为如何?它确实兼容,或者我在风格指南中误读了某些东西?

在此先感谢。

回答

0

我认为你误读了风格指南。在共享组件和指令下,应该只放置嵌套在多个父组件中的组件和指令。即如果您有网格组件和产品列表组件的标头组件,则应将标头组件放在共享文件夹中。或者一些类似的组件嵌套在多个其他组件中,而不是独立组件,或者说“功能组件”。另一个例子是您可以与不同组件共享的服务。最后,它并不是那么重要,因为只有结构的目的是快速找到和定位组件。

+0

感谢您的快速回复我的朋友!我明白你的观点,但我仍然有点困惑。在对最初的问题进行了一些编辑之后:'module1-list.component'和'module1-view.component'都嵌套在/在'foo-container.component'和'bar-container.component'组件下使用。此外,'module1-container.component'是一个延迟加载组件(因此文件夹名称中的“+”),因为它是使用路由器加载的。但是,'module1-list.component'应该能够从惰性和非惰性加载组件加载;因此,在懒惰加载文件夹下有点感觉不对。 – AstrOne