如何避免在angularoutes中的多个声明

时间:2017-02-28 09:42:30

标签: angular typescript angular2-routing

我的应用模块

/* dependency Modules */
import { NgModule } from '@angular/core'

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

/* router configuration */
import { AppRoutingModule } from './app.routes';

/* components */
import { AppComponent } from './app.component';

/* project Modules */
import { subscriptionModule } from '../components/subscription/subscription.module';

/* app declaration */
@NgModule({
  declarations: [
    AppComponent
  ],
  imports: [
    BrowserModule,
    FormsModule,
    ReactiveFormsModule,
    HttpModule,
    AppRoutingModule,
    subscriptionModule,
  ],
  providers: [],
  bootstrap: [AppComponent]
})
export class AppModule {

}

我的路线配置

/* dependency modules */
import { NgModule } from '@angular/core'
import { Routes } from '@angular/router';
import { RouterModule } from '@angular/router';

/* subscription */
/*components */
import { subscriptionComponent } from '../components/subscription/subscription.component';
import { subscriptionMenuComponent } from './../components/subscription/includes/menu/subscriptionMenu.component';
import { subscriptionSubscribeComponent } from './../components/subscription/includes/subscribe/subscriptionSubscribe.component';

export const rootRouterConfig: Routes = [
  { path: '', redirectTo: 'subscription', pathMatch: 'full' },
  {
    path: 'subscription', component: subscriptionComponent,
    children: [
      { path: '', component: subscriptionMenuComponent },
      { path: 'subscribe', component: subscriptionSubscribeComponent },
    ]
  },
];

@NgModule({
  imports: [
    RouterModule.forRoot(rootRouterConfig, { useHash: true }),
  ],
  exports: [
    RouterModule
  ]
})
export class AppRoutingModule { }

我的订阅模块

/* dependency modules */
import { NgModule } from '@angular/core';
import { RouterModule } from '@angular/router';

/* components */
import { subscriptionComponent } from './subscription.component';
import { subscriptionHeaderComponent } from './header/subscriptionHeader.component';
import { subscriptionMenuComponent } from './includes/menu/subscriptionMenu.component';
import { subscriptionSubscribeComponent } from './includes/subscribe/subscriptionSubscribe.component';


@NgModule({
  imports: [
    RouterModule
  ],
  declarations: [
    subscriptionComponent,
    subscriptionHeaderComponent,
    subscriptionMenuComponent,
    subscriptionSubscribeComponent
  ]
})

export class subscriptionModule { }

在我的代码中,我已经在Subscription模块中导入了组件,我是否必须在路由器配置中重新导入它。我对角度2是全新的,有人可以告诉我,我所做的是正确的。页面正确加载,但是有任何可能的方法来减少导入。

1 个答案:

答案 0 :(得分:0)

  • ngModule中,您必须导入您在该模块中使用的所有组件

  • 但您还必须导入路由中使用的所有组件。

  • 但是在延迟加载的情况下,不需要在路由文件和模块中导入组件。