功能模块路由具有相同的父布局角

时间:2018-01-14 13:54:11

标签: angular typescript angular2-routing

我想为不同的功能模块使用相同的布局(在 app.module.ts 中定义),每个模块都有自己的路由。还有一个单独的登录/注册布局,没有侧面菜单,页眉和页脚。到目前为止我试过这个:

//../app/app.component.html
<router-outlet></router-outlet> // here i want login and layout view.

和布局组件:

//../app/layout.component.html
<header></header>
<side-menu></side-menu>
<router-outlet></router-outlet> // here i want layout views that would have side menu with them e.g. dashboard, inventory etc
<footer></footer>

仪表板路线在app.module.ts中,但库存和其他模块有自己的模块和路线,如下所示:

//app.module.ts
const appRoutes: Routes = [
    { path: 'login', component: LoginComponent },
    {
        path: '',
        component: LayoutComponent,
        children: [
            { path: '', component: DashboardOne},
            { path: 'dashboardOne', component: DashboardOne},
            { path: 'dashboardTwo', component: DashboardTwo}
        ],
        canActivate: [AuthGuard]
    }
];    
@NgModule({
  declarations: [
    AppComponent,
    DashboardOneComponent,
    DashboardTwoComponent,
    LoginComponent,
    LayoutComponent
  ],
  imports: [
    RouterModule.forRoot(
      appRoutes,
      { enableTracing: true } // <-- debugging purposes only
    ),
    InventoryModule,
    WarehouseModule,
    UserModule,
  ],
  providers: [AuthGuard],
  bootstrap: [AppComponent]
})
export class AppModule { }

和另一个模块:

//inventory.module.ts
const appRoutes: Routes = [
    {
        path: 'inventory',
        //component: LayoutComponent,
        children: [
            { path: '', component: InventoryOne},
            { path: 'inventoryOne', component: InventoryOne},
            { path: 'inventoryTwo', component: InventoryTwo}
        ],
        canActivate: [AuthGuard]
    }
];
@NgModule({
  declarations: [
    AppComponent,
    InventoryOneComponent,
    InventoryTwoComponent,
    //LayoutComponent // want to use this layout in other modules too
  ],
  imports: [
    RouterModule.forChild(
      appRoutes
    ),
  ],
  providers: [],
})
export class InventoryModule { }

如果我从库存模块中删除布局组件的注释,它会重新呈现布局组件(我不想要)我想在每个具有自己路由的模块中使用layoutComponent,并且到目前为止无法做到如此。

1 个答案:

答案 0 :(得分:1)

AppModule中,您可以默认重定向到FeaturesModule,其中包含LayoutComponent的菜单,而AuthGuard可以在需要时重定向到/login

const appRoutes: Routes = [
  {        
    path: '',
    loadChildren: '../<insertyourpath>/features.module#FeaturesModule',
    canActivate: [AuthGuard]
  },
  {
     path: 'login',
     component: LoginComponent
  }
];

FeaturesModule中,您将在LayoutComponent的插座中呈现要素路径:

const featureRoutes: Routes = [
  {
    path: '',
    component: LayoutComponent,
    children: [
      {        
        path: 'inventory',
        loadChildren: '../<insertyourpath>/inventory.module#InventoryModule'
      }
    ]
  }
];

InventoryModule中,您放置了所有模块的子路由(分别为其他FeatureModule)。必须将DashBoard移动到FeaturesModule或其自己的模块。:

const inventoryRoutes: [
  { path: '', component: InventoryOne},
  { path: 'inventoryOne', component: InventoryOne},
  { path: 'inventoryTwo', component: InventoryTwo}
];

请注意,使用给定的loadChildren语法,引用的模块将延迟加载。如果您希望同步加载它,请查看此SO answer