Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Angular 8 Nested routes and multiple router outlet doesn't work

I have a simple angular 8.3 application for now but the routing does not work.

When I go to /logs I have no errors but nothing is displayed to the screen.

When I go to /logs/detailed/1036 there is an error in the console : "Error: Cannot match any routes. URL Segment: 'logs/detailed/1036'"

I tried a lot of differents solution that I found on internet but nothing works. What am I missing ?

Here is a simple tree of my application :

/app
- app.module.ts
- app-routing.module.ts
- app.component.html
      /log-page
      - log-page.module.ts
      - log-page-routing.module.ts
      - log-page.component.html

app.module.ts

@NgModule({
   declarations: [
      AppComponent
   ],
   imports: [
      BrowserModule,
      BrowserAnimationsModule,
      HttpClientModule,
      AppRoutingModule,
      SharedModule,
      LogPageModule,
      ToastrModule.forRoot(),
      TabMenuModule
   ],
   providers: [],
   bootstrap: [
      AppComponent,
   ]
})
export class AppModule { }

app-routing.module.ts

const routes: Routes = [
  { path: '', redirectTo: 'logs', pathMatch: 'full'},
  { path: 'logs',  loadChildren: () => import(`./log-page/log-page.module`).then(m => m.LogPageModule)},
];

@NgModule({
  imports: [RouterModule.forRoot(routes, {enableTracing: true})],
  exports: [RouterModule]
})
export class AppRoutingModule { }

app.component.html

<app-header></app-header>
<p-tabMenu [model]="navigationItems"></p-tabMenu>
<router-outlet></router-outlet>

log-page.module.ts

@NgModule({
  imports: [
    CommonModule,
    SpinnerModule,
    MenuModule,
    FormsModule,
    ButtonModule,
    TableModule,
    InputTextModule,
    SharedModule,
    LogPageRoutingModule
  ],
  declarations: [
    LogPageComponent,
    FilterBarComponent,
    LogTableComponent,
    DetailedLogComponent,
    ErrorStatusComponent
],
  providers: [
    FilterSettingsService
  ]
})
export class LogPageModule { }

log-page-routing.module.ts

const routes: Routes = [
    {
        path: '', outlet: 'log-page', component: LogTableComponent, children: [
            {
                path: 'detailed/:logId', outlet: 'log-page', component: DetailedLogComponent
            }, {
                path: '**', redirectTo: '', pathMatch: 'full'
            }
        ]
    }
];

@NgModule({
    imports: [RouterModule.forChild(routes)],
    exports: [RouterModule]
})
export class LogPageRoutingModule { }

log-page.component.ts

<div class="p-grid ">
    <div class="p-col-fixed" style="width: 200px;">
        <app-error-status></app-error-status>
    </div>
    <div class="p-col">
        <router-outlet name="log-page"></router-outlet>
    </div>
</div>
like image 486
xqlimax Avatar asked Nov 20 '19 12:11

xqlimax


People also ask

Can Angular 8 use multiple router outlets?

Multiple Outlets And Auxiliary Routes #Angular Router supports multiple outlets in the same application. A component has one associated primary route and can have auxiliary routes. Auxiliary routes enable developers to navigate multiple routes at the same time.

Can we have multiple router outlet in Angular?

You can have multiple router-outlet in same template by configuring your router and providing name to your router-outlet, you can achieve this as follows. Advantage of below approach is thats you can avoid dirty looking URL with it.

What does RouterLink do in Angular?

In Angular, RouterLink is a directive for navigating to a different route declaratively. Router. navigate and Router. navigateByURL are two methods available to the Router class to navigate imperatively in your component classes.


1 Answers

Solution 1

Try without a named router outlet. Is there any specific reason why you are using a named outlet? There is a known bug that says that lazy loading and named outlets don't work well together

You don't need a named outlet to have nested router outlets. You only need them if they are in the same component and you want to practically expose two different components in the same place, without one being the child of the other.

Solution 2

If named router outlet is the only way to go, there is another answer here that might work for you. Check detailed solution here

like image 65
Athanasios Kataras Avatar answered Sep 27 '22 21:09

Athanasios Kataras