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

Asa*_*hah 5 typescript angular2-routing angular

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

//../app/app.component.html
<router-outlet></router-outlet> // here i want login and layout view.
Run Code Online (Sandbox Code Playgroud)

和一个布局组件:

//../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>
Run Code Online (Sandbox Code Playgroud)

仪表板路由在 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 { }
Run Code Online (Sandbox Code Playgroud)

和另一个模块:

//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 { }
Run Code Online (Sandbox Code Playgroud)

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

Kim*_*ern 2

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

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

在您的中,FeaturesModule您将在以下的出口中呈现特征路径LayoutComponent

const featureRoutes: Routes = [
  {
    path: '',
    component: LayoutComponent,
    children: [
      {        
        path: 'inventory',
        loadChildren: '../<insertyourpath>/inventory.module#InventoryModule'
      }
    ]
  }
];
Run Code Online (Sandbox Code Playgroud)

在你的模块中InventoryModule,你可以放置所有模块的子路由(分别是其他FeatureModules)。仪表板必须移动到 FeaturesModule 或其自己的模块。:

const inventoryRoutes: [
  { path: '', component: InventoryOne},
  { path: 'inventoryOne', component: InventoryOne},
  { path: 'inventoryTwo', component: InventoryTwo}
];
Run Code Online (Sandbox Code Playgroud)

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

  • 是的,金,这就是我正在寻找的。我在您编辑之前就弄清楚了,因为您的回答帮助我了解了我的不足之处。 (2认同)