Search code examples
htmlangulartypescriptroutesangular-routing

Angular Multiple layouts routing is not working


I have the follwing multi-layout structure without lazy loading. When navigating to https://localhost:44327/admin or /student it doesn't show anything. If I use an empty path instead of admin and student then navigate to it's child (/manageConcern) route it loads the first (AdminLayoutComponent) Layout specified in routing. The second LayoutCompnent (/trainings) doesn't load. looks like the router-outlet in app.component can't read the paths. Any idea what I am missing here? This is what I want to achieve https://stackblitz.com/edit/angular-multi-layout-example

Routing-module.ts

RouterModule.forRoot([
  {
    path: 'admin',
    component: AdminLayoutComponent,
    children: [
      {path: '', redirectTo: 'manageConcern', pathMatch: 'full'},
      {path: 'manageConcern', component: MasterConcernComponent},
      {path: 'createTraining', component: AdminComponent},
    ]
  },
  {
    path: 'student',
    component: StudentLayoutComponent,
    children: [
      {path: '', redirectTo: 'trainings', pathMatch: 'full'},
      {path: 'trainings', component: TrainingsComponent},
    ]
  }
]),

AdminLayoutComponent.html

<app-header></app-header>
    <app-menu [menuItems]="menuItems"></app-menu>
    <div class="app-body-content m-5 p-4">
       <router-outlet></router-outlet>
    </div>

StudentLayoutComponent.html

    <app-menu [menuItems]="menuItems"></app-menu>
    <div class="app-body-content m-5 p-4">
       <router-outlet></router-outlet>
    </div>

app.component.html

 <router-outlet></router-outlet>

Solution

  • As app.component.ts is not a routed component it doesn't know anything about the active route. If you want to use your main router-outlet in app.component.ts then you have to add <base href="/"> in your index.html.