Angular2 - 项目结构

Angular2 - Project Structure

我将开始一个新的 Angular2 项目,我正在尝试了解 Angular2 应用程序的最佳结构。让我的页面为 homeauto-galleriesnearest-galleriesbrandscarsselected-car。并且导航顺序是

home -> auto-galleries -> nearest-galleries

home -> brands -> cars -> selected-car

为获得最佳方法,我应该为每个页面使用组件还是模块?如果模块是更好的方法,我应该使用分层模块还是都在根模块下的同一级别?

例如下面的结构有多好:

app
├--- app.module.ts
├--- app.component.ts
├--- home.html
├--- brands
|    ├--- brands.module.ts
|    ├--- brands.component.ts
|    ├--- brands.html
|    ├--- cars
|         ├--- cars.module.ts
|         ├--- cars.component.ts
|         ├--- cars.html
|         ├--- selected-car
|              ├--- selected-car.module.ts
|              ├--- selected-car.component.ts
|              ├--- selected-car.html
|
├--- auto-galleries
     ├--- auto-galleries.module.ts
     ├--- auto-galleries.component.ts
     ├--- auto-galleries.html
     ├--- nearest-galleries
          ├--- nearest-galleries.module.ts
          ├--- nearest-galleries.component.ts
          ├--- nearest-galleries.html

或者这个结构更好:

app
├--- app.module.ts
├--- app.component.ts
├--- home.html
├--- brands
|    ├--- brands.module.ts
|    ├--- brands.component.ts
|    ├--- brands.html
|
├--- cars
|    ├--- cars.module.ts
|    ├--- cars.component.ts
|    ├--- cars.html
|
├--- selected-car
|    ├--- selected-car.module.ts
|    ├--- selected-car.component.ts
|    ├--- selected-car.html
|
├--- auto-galleries
|    ├--- auto-galleries.module.ts
|    ├--- auto-galleries.component.ts
|    ├--- auto-galleries.html
|
├--- nearest-galleries
     ├--- nearest-galleries.module.ts
     ├--- nearest-galleries.component.ts
     ├--- nearest-galleries.html

注意:这只是一个简单的例子,我的应用程序更适合模块化结构:)

我将使用第二个文件来处理文档根目录下文件夹中的所有文件。 这种结构使您更容易避免被大量子文件夹淹没。

此结构也更易于维护且更易于阅读。

两种方法都很好。但是您不需要在每个文件夹中使用 module.ts。请阅读此文档 http://blog.angular-university.io/angular2-ngmodule/。 所以我相信您需要 1 个根模块和 2 个子模块可能用于品牌,auto-gallery。

如果您使用的是第一种方法。让你的 index.ts 在根级别简化路由。有子路由。

首先,在我看来,使用模块绝对是一种更好的方法,因为您将避免大量样板代码(这是无论如何都制作模块的原因之一)。

我建议使用第二种结构,因为它不那么令人困惑,而且我相信在不久的将来维护起来会容易得多。

我建议添加到第二个结构的是更多全局文件夹,如 "Car" 文件夹,所有与汽车相关的 sub-folders 都将放在该文件夹中。这样你就可以只创建一个汽车模块,任何与汽车相关的东西都将存储在该模块中。我真的不觉得每个与汽车相关的功能都需要一个单独的模块。 (喜欢selected-car.module.ts)

Here您可以了解更多关于目录结构和诸如共享模块之类的内容,这也会使您的应用程序更加简洁。

Angular 文档在 Style Guide:

中对此有一些建议
<project root>
  src
    app
      core
        core.module.ts
        exception.service.ts|spec.ts
        user-profile.service.ts|spec.ts
      heroes
        hero
          hero.component.ts|html|css|spec.ts
        hero-list
          hero-list.component.ts|html|css|spec.ts
        shared
          hero-button.component.ts|html|css|spec.ts
          hero.model.ts
          hero.service.ts|spec.ts
        heroes.component.ts|html|css|spec.ts
        heroes.module.ts
        heroes-routing.module.ts
      shared
        shared.module.ts
        init-caps.pipe.ts|spec.ts
        text-filter.component.ts|spec.ts
        text-filter.service.ts|spec.ts
      villains
        villain
          ...
        villain-list
          ...
        shared
          ...
        villains.component.ts|html|css|spec.ts
        villains.module.ts
        villains-routing.module.ts
      app.component.ts|html|css|spec.ts
      app.module.ts
      app-routing.module.ts
    main.ts
    index.html
    ...
  node_modules/...
  ...

+Style-04-04:

Do keep a flat folder structure as long as possible.