Feature modules routing with same parent layout angular

守給你的承諾、 提交于 2019-12-12 19:09:54

问题


I want to use same layout (that is defined in app.module.ts) for different feature modules, each module has its own routing. And a separate login/register layout that would not have side menu, header and footer. So far i tried this:

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

and a layout component:

//../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>

dashboard routes are in app.module.ts but the inventory and other modules has there own modules and routes as shown below:

//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 { }

and the other module:

//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 { }

if i remove the comment for layout component from inventory module it re-renders the layout component (I don't want that) i want to use layoutComponent in every module that has its own routes and so far unable to do so.


回答1:


In your AppModule you can redirect to the FeaturesModule by default, which will have the menu from the LayoutComponent, whereas the AuthGuard can redirect to /login when needed.

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

In your FeaturesModule you'll have the feature paths rendered in the outlet of the LayoutComponent:

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

In your InventoryModule you put all the module's child routes (other FeatureModules respectively). The DashBoard has to be moved to the FeaturesModule or to its own module.:

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

Note that with the given loadChildren syntax, the referenced module will be lazy loaded. If you want it to be loaded synchronously check out this SO answer.



来源:https://stackoverflow.com/questions/48250269/feature-modules-routing-with-same-parent-layout-angular

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!