Angular2管理大量组件

时间:2017-03-23 09:35:02

标签: angular angular2-components angular2-modules

我正在研究Angular2。我们知道,我们需要在主模块中配置所有providerscomponentsservices。可能是我们有大量components并手动配置它的情况。例如,请考虑以下module

 import { NgModule } from '@angular/core';
 import { FormsModule } from '@angular/forms';
 import { BrowserModule } from '@angular/platform-browser';
 import { HttpModule } from '@angular/http';
 import { AppComponent } from './app.component';
 import { GoogleSigninComponent } from ./auth/google.login.component';
 import { ApiEndpoints } from './api.endpoints';
 // Providers
 import { AuthService } from './auth/auth.service';
 import { AppRoutingModule } from './route';
 import { ExpenseComponent } from './expense/expense.component';
 import { ExpenseService } from './expense/expense.service';

 import { GroupService } from './group/group.service';
 import {GroupUserService} from './group/group-user.service';
 import {MainGroupComponent, SaveGroupComponent, GroupListComponent } from './group/group.components';
 import {GroupUsersComponent,AddGroupUserComponent} from './group/group-  user.component';
 import {HelperService } from './helper.service';
 import {SearchUserComponent} from './user/users.component';
 import {UserService} from './user/users.service';


@NgModule({
  imports: [BrowserModule, HttpModule, BrowserModule, AppRoutingModule, FormsModule],
  declarations: [AppComponent, GoogleSigninComponent,
  ExpenseComponent, SaveGroupComponent, GroupListComponent, GroupUsersComponent,
MainGroupComponent,AddGroupUserComponent,SearchUserComponent],
 bootstrap: [AppComponent],
 providers: [AuthService, ApiEndpoints, ExpenseService,
 GroupService,GroupUserService,HelperService,UserService]
  })

我需要在routescubersome中配置相同内容 在没有太多复制粘贴的情况下配置大量组件的正确方法是什么?

另外,我相信应该有多个模块。您的建议或正确方法是什么?

1 个答案:

答案 0 :(得分:0)

对于服务,创建一个单独的shared.ts文件并导入&出口所有服务。因此,对于每个文件,像这样创建一个.ts文件 例如,请参阅以下代码

import { GroupService } from './group/group.service';
import {GroupUserService} from './group/group-user.service';

const SHARED_PROVIDERS: any[] = [
  GroupService ,
  GroupUserService
];

    export{
    Shared_Providers,
    GroupService ,
    GroupUserService

    }