Search code examples
angulardesign-patternsproject-structure

Angular2 - Project Structure


I will start to a new Angular2 project and I am trying to understand the best structure for an Angular2 application. Let I have pages as home, auto-galleries, nearest-galleries, brands, cars and selected-car. And the navigation order is

home -> auto-galleries -> nearest-galleries

or

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

For the best approach, should I use components or modules for each page? If modules are a better approach, should I use hierarchical modules or all at same level under the root module?

For example, how good is the structure below:

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

Or is this structure better:

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

Note: This is just a simple example, my application better fits a modular structure :)


Solution

  • Angular Docs have some recommendations to this in their 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.