简体   繁体   English

Angular2 - 项目结构

[英]Angular2 - Project Structure

I will start to a new Angular2 project and I am trying to understand the best structure for an Angular2 application. 我将开始一个新的Angular2项目,我试图了解Angular2应用程序的最佳结构。 Let I have pages as home , auto-galleries , nearest-galleries , brands , cars and selected-car . 让我有homeauto-galleriesnearest-galleriesbrandscarsselected-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 :) 注意:这只是一个简单的例子,我的应用程序更适合模块化结构:)

I would use the second one with all files in folders on the document root. 我会使用第二个文件根目录中的文件夹中的所有文件。 This structure makes it much easier not to get overhelmed by the lot of subfolders. 这种结构使得更容易不被大量子文件夹所淹没。

This structure is also more maintainable and easier to read. 这种结构也更易于维护和阅读。

Both approaches are good. 两种方法都很好。 But you don't need module.ts in each folder. 但是你不需要在每个文件夹中使用module.ts。 Please read this doc http://blog.angular-university.io/angular2-ngmodule/ . 请阅读此文档http://blog.angular-university.io/angular2-ngmodule/ So I believe you need 1 root module and 2 child module may be for brand,auto-gallery. 所以我相信你需要1个根模块和2个子模块可能是品牌,自动画廊。

If you are using 1st approach. 如果您使用第一种方法。 Make your of index.ts What are all the index.ts used for? 使你的index.ts 所有index.ts用于什么? To simplify routes at root level. 简化根级别的路由。 Have child routes. 有孩子的路线。

First of all, definitely going with modules is a better approach in my opinion, because you will avoid a lot of boilerplate code(which is one of the reasons why modules were made anyway). 首先,在我看来,明确使用模块是一种更好的方法,因为你将避免大量样板代码(这也是模块制作的原因之一)。

I would suggest going with the second structure since it's less confusing and I believe it would be much easier to maintain in the near future. 我建议使用第二种结构,因为它不那么混乱,我相信在不久的将来维护会更容易。

The thing I would suggest adding to the second structure is more global folders like "Car" folder where all the car related sub-folders would go. 我建议添加到第二个结构的东西是更多的全局文件夹,如“Car”文件夹,其中所有与汽车相关的子文件夹都会出现。 This way you can create just a car module and anything car related would be stored in that module. 这样您就可以创建一个汽车模块,并且任何与汽车相关的东西都将存储在该模块中。 I don't really feel like you need a separate module for each car related feature. 对于每个与汽车相关的功能,我真的不觉得你需要一个单独的模块。 (Like selected-car.module.ts) (比如selected-car.module.ts)

Here you can learn a bit more about directory structures and things like shared module, which would also keep your app a lot cleaner. 在这里,您可以了解更多关于目录结构和共享模块等内容,这也可以使您的应用程序更加清晰。

Angular Docs have some recommendations to this in their Style Guide : Angular Docs在其风格指南中对此提出了一些建议:

<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 : + Style-04-04

Do keep a flat folder structure as long as possible. 不要保持一个平面文件夹结构尽可能长的时间。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM