简体   繁体   English

Angular2如何清理AppModule

[英]Angular2 How to clean up the AppModule

I've been using the tutorials online and created an 'ok' SPA data entry application. 我一直在线使用这些教程并创建了一个'ok'SPA数据输入应用程序。

I have it connecting to my WEB API fine but have only built out one Model and already my AppModule has quiet a few lines. 我把它连接到我的WEB API很好,但只构建了一个模型,我的AppModule已经安静了几行。

I'm thinking forward and using the current method I think the AppModule will be a crazy size once i finish with it, tough to read and possibly even tougher to debug. 我正在考虑使用当前的方法,我认为一旦我完成它,AppModule将是一个疯狂的大小,难以阅读,甚至可能更难调试。

Have I possibly missed the point of how to structure an Angular2 larger application? 我是否可能错过了如何构建Angular2更大应用程序的观点?

I'm struggling to find a tutorial/project online that is larger than 1 component for reference. 我正在努力寻找一个大于1个组件的在线教程/项目供参考。

Below is my app.module.ts and folder structure. 下面是我的app.module.ts和文件夹结构。

I separate my CashMovement , ListComponent and DataService which I would think is good practice but add another 10 different data-services and lists and the app.module will be lengthy. 我将CashMovementListComponentDataService分开,我认为这是一个很好的做法,但是添加另外10个不同的数据服务和列表,app.module将会很长。

Before I proceed any further does anybody please have some reading they could point me towards or advice which I understand is subjective to personal opinion. 在我继续进行任何进一步的工作之前,任何人都应该阅读一些他们可以指向我的建议,或者我理解的建议对个人意见是主观的。

app.module app.module

import './rxjs-operators';

import { NgModule }      from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';
import { FormsModule } from '@angular/forms';
import { HttpModule }    from '@angular/http';

import { PaginationModule, DatepickerModule, Ng2BootstrapModule, ModalModule, ProgressbarModule, TimepickerModule } from 'ng2-bootstrap/ng2-bootstrap';

import { SlimLoadingBarService, SlimLoadingBarComponent } from 'ng2-slim-loading-bar';


import { AppComponent }   from './app.component';
import { DateFormatPipe } from './shared/pipes/date-format.pipe';
import { HighlightDirective } from './shared/directives/highlight.directive';
import { HomeComponent } from './home/home.component';
import { MobileHideDirective } from './shared/directives/mobile-hide.directive';

import { CashMovementListComponent } from './cashmovements/cashmovement-list.component';
import { CashMovementDataService } from './cashmovements/cashmovement.data.service';

import { routing } from './app.routes';

import { ConfigService } from './shared/utils/config.service';
import { ItemsService } from './shared/utils/items.service';
import { MappingService } from './shared/utils/mapping.service';
import { NotificationService } from './shared/utils/notification.service';

@NgModule({
    imports: [
        BrowserModule,
        DatepickerModule,
        FormsModule,
        HttpModule,
        Ng2BootstrapModule,
        ModalModule,
        ProgressbarModule,
        PaginationModule,
        routing,
        TimepickerModule
    ],
    declarations: [
        AppComponent,
        DateFormatPipe,
        HighlightDirective,
        HomeComponent,
        MobileHideDirective,
        SlimLoadingBarComponent,
        CashMovementListComponent        
    ],
    providers: [
        ConfigService,
        CashMovementDataService,
        ItemsService,
        MappingService,
        NotificationService,
        SlimLoadingBarService
    ],
    bootstrap: [AppComponent]
})
export class AppModule { }

Folder Structure 文件夹结构

在此输入图像描述

You need to learn to make use of modules. 您需要学习如何使用模块。

I usually break the modules down into these types 我通常将模块分解为这些类型

  • Layout modules 布局模块
  • Feature modules 功能模块
  • Core module (only 1) 核心模块(仅限1个)
  • Shared module (only 1) 共享模块(仅限1个)
  • App module (only 1) 应用模块(仅限1个)

Layout module is for laying out the app. 布局模块用于布置应用程序。 For example a topbar module, a sidemenu module, a footer module, and a main content module. 例如,顶部模块,侧面菜单模块,页脚模块和主内容模块。

Feature module . 功能模块 What exactly is this? 究竟是什么? There's really no clear definition, but whatever feature area you feel can be self contained in module, you might as well do it. 实际上没有明确的定义,但无论您认为哪个功能区域都可以自包含在模块中,您也可以这样做。 You will import these feature modules into your layout modules, as the features are what make up the different layout components 您将这些要素模块导入到布局模块中,因为这些要素构成了不同的布局组件

Core module . 核心模块 Here you will export your layout modules, and all your core (singleton) services. 在这里,您将导出布局模块和所有核心(单件)服务。 You only need to export (and not import) the modules, as nothing in the core module will actually use those layout modules. 您只需要导出 (而不是导入)模块,因为核心模块中的任何内容都不会实际使用这些布局模块。 You just export them so that the app module can use them. 您只需导出它们,以便应用程序模块可以使用它们。 The core module will only be imported into the app module 核心模块将仅导入到app模块中

Shared module . 共享模块 This is where you will declare all your shared pipes, directives, and components. 您可以在此处声明所有共享管道,指令和组件。 Also you you can export commonly used modules like CommonModule and FormsModule . 您还可以导出常用的模块,如CommonModuleFormsModule Other modules will be using the module 其他模块将使用该模块

App module . 应用模块 You already know what this is. 你已经知道这是什么了。 Out of your own created modules, the only ones you need to import are the shared and core modules. 在您自己创建的模块中,您需要导入的唯一模块是共享模块和核心模块。

Here's a basic layout 这是一个基本布局

SharedModule SharedModule

@NgModule({
  declarations: [ HighlightDirective, SharedPipe, SharedComponent ],
  exports: [ 
    HighlightDirective, SharedPipe, SharedComponent,
    CommonModule, FormsModule
  ]
})
class SharedModule {}

Layout Modules Notice other modules will use the SharedModule 布局模块注意其他模块将使用SharedModule

@NgModule({
  imports: [ FeatureAModule, FeatureBModule, SharedModule ]
  declarations: [ TopbarComponent ],
  exports: [ TopbarComponent ]
})
class TopbarModule {}

@NgModule({
  imports: [ SharedModule ]
  declarations: [ SidemenuComponent ],
  exports: [ SidemenuComponent ]
})
class SidemenuModule {
  static forRoot() {   // pattern for adding app-wide services
    return {
      ngModule: SidemenuModule,
      providers: [ MenuSelectionService ]
    }
  }
}

@NgModule({
  imports: [ HomeModule, OtherModule, SharedModuel ]
  declarations: [ MainContentComponent ],
  exports: [ MainContentComponent ]
})
class MainContentModule {}

CoreModule Bring together all the layout modules that make up the application. CoreModule将构成应用程序的所有布局模块组合在一起。 And also add other app-wide services, that are not tied to other modules 并且还添加了与其他模块无关的其他应用程序范围的服务

@NgModule({
  imports: [ SidemeuModule.forRoot() ]
  exports: [ TopbarModule, SidemenuModule, MainContentModule ],
})
class CoreModule {
  static forRoot() {
    return {
      ngModule: CoreModule,
      providers: [ UserService, AuthService ]
    }
  }
}

AppModule 的AppModule

@NgModule({
  imports: [
    BrowserModule,
    SharedModule,
    CoreModule.forRoot(),  // forRoot so we get all the providers
    HttpModule,
    RouterModule.forRoot(APP_ROUTES)
  ],
  declarations: [ AppComponent ],
  bootstrap: [ AppComponent ]
})
class AppModule {}

See Also: 也可以看看:

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

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