Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Feature modules routing with same parent layout angular

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.

like image 854
Asad Shah Avatar asked Oct 28 '22 20:10

Asad Shah


1 Answers

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.

like image 66
Kim Kern Avatar answered Nov 08 '22 06:11

Kim Kern