我正在研究Angular2。众所周知,我们需要配置所有 providers
,components
,services
在主模块中。可能是我们有大量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]
})
我需要在routes
和有点cubersome
中配置相同的 在不进行大量复制粘贴的情况下配置大量组件的正确方法是什么?
另外,我相信应该有多个模块。您的建议或正确的方法是什么?
对于服务,创建一个单独的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
}