Troubleshooting the issue of Angular 8 nested routes and multiple router outlet functionality not functioning as intended

I am currently working on an Angular 8.3 application, but I am facing issues with the routing functionality.

When navigating to /logs, there are no errors displayed on the screen.

However, accessing /logs/detailed/1036 results in a console error: "Error: Cannot match any routes. URL Segment: 'logs/detailed/1036'".

Despite trying various solutions found online, I am unable to resolve the issue. What could I be overlooking?

Below is a basic hierarchy 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>

Answer №1

Best Approach

Consider using nested router outlets without a named outlet. Is there a specific reason you are utilizing a named outlet? It's worth noting that lazy loading and named outlets may not function well together due to a known bug referenced here

Named outlets are not necessary for nested router outlets; they are only required when exposing two distinct components within the same location, independent of parent-child relationships.

Alternative Solution

If a named router outlet is unavoidable, another potential solution can be found in this detailed explanation.

Answer №2

After implementing a few adjustments, the functionality of my application has improved slightly but an issue still remains unresolved.

Below is the content of my app-routing.module.ts file:

{ path: '', redirectTo: 'logs', pathMatch: 'full'},
{ path: 'logs', component: LogPageComponent , loadChildren: () => import(`./log-page/log-page.module`).then(m => m.LogPageModule)}

Furthermore, here is the content of my log-page-routing.module.ts file:

{ path: '', outlet: 'log-page', component: LogTableComponent },
{ path: 'detailed', outlet: 'log-page', component: DetailedLogComponent }

Although I am able to successfully load the logs, encountering an error when attempting to navigate to the 'logs/detailed' route. The error message received is: "Error: Cannot match any routes. URL Segment: 'logs/detailed/"

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Convert an array into a JSON object for an API by serializing it

Currently, I am working with Angular 12 within my TS file and have encountered an array response from a file upload that looks like this- [ { "id": "7", "name": "xyz", "job": "doctor" ...

What is the best way to showcase nested array JSON data in an HTML Table?

https://i.stack.imgur.com/OHL0A.png I attempted to access the following link http://jsfiddle.net/jlspake/v2L1ny8r/7/ but without any success. This is my TypeScript code: var viewModel = function(data){ var self = this; self.orders = ko.observableArr ...

"Passing Data from Angular Parent Component to Child Component

At first, the parent component and child don't have any connection. It's just a list of elements. However, when the user clicks on the list, the child component is loaded. Then, I can call a method of the child component from the parent using @Vi ...

Tips for dynamically replacing the <span style="text-decoration: underline;"> </span> with the <u></u> tag in Angular 6

Users are able to enter text into an input box and apply formatting such as font name, bold, italic, underline, and font size for selected text. When a user underlines any word or sentence, the source code for that styling appears as <span style="t ...

Struggles with fake XmlHttpRequests in Karma during Angular testing tasks

Currently, I am facing a challenge in testing an Angular service (built on feathersJS) using the standard Angular testing tools - Karma and Jasmine. Since my service involves XHR calls to a backend through socket.io, I am looking to mock these calls for t ...

How to Add RouterModule to an Angular 10 Library without Ivy

I am developing a custom Angular 10 Library to be shared on a private NPM repository. I followed the instructions from Angular.io and used ng new my-workspace --create-application=false and ng generate library my-library with Angular CLI. In one of the co ...

Invoke object as a function (JavaScript/TypeScript)

If I have a logging class named Logger, can I define a special method within the class to be called when the instance of the class is used as a function? So instead of using log.method(), it would be possible to use log() directly like this: log(...) In ...

Angular 11 project facing issues with Bootstrap 5 tooltip functionality

Embracing the power of Bootstrap 5.0.2 in an Angular 11 project, I included the following code: index.html <!doctype html> <html lang="en"> <head> <meta charset="utf-8"> <meta http-equiv="X-UA-Compa ...

Pressing the tab key while using Angular will halt any additional processes from running

I recently integrated a search bar into my Angular application. When I enter text in the input field, a customized dropdown menu appears below it. However, when I press the tab key, I expect the dropdown to close and for the focus to shift to the next inpu ...

What causes JavaScript class instance properties to become null after being set?

Developing a nodejs / express MVC app, I have created a custom model named MyModel (let's just pretend that's the actual name for privacy reasons). The properties of this class are initialized as null by default: export class MyModel { /* -- ...

Leveraging the power of Angular's function within ElementRef and accessing the

Is there a way to access an Angular function using ElementRef? ngAfterViewInit() { let _self = this; this.datatable.on('m-datatable--on-layout-updated', function(e){ $(_self.elRef.nativeElement).find('.deleteFn').c ...

Retrieve the input value from a Bootstrap Form Input

When using a Bootstrap Form Input to allow the user to enter text, I am wondering how to save that text and use it as a parameter in a function. Below is the HTML snippet I have: <!--Directory Input Form--> <div class="row"> <div class= ...

The MemoizedSelector<ICommonAppState, IMenuItemsObject[]> argument does not match the expected 'string' parameter type

I have implemented the use of createSelector from @ngrx/store in order to select an array of objects from my store. Despite successfully compiling my application, I encountered the following error: Argument of type 'MemoizedSelector<ICommonAppSta ...

What steps can I take to stop typescript tsc from compiling the ../node_modules directory?

I'm currently working on a project and I decided to clone the typescript/node/express sample project into a subfolder. However, when I try running tsc -p . in the sub-folder, I encounter compilation errors because tsc is scanning ../node_modules. Is t ...

Automatically align a Div to the right within an ngFor directive in an Angular 6 application

<div class="row"> <div class="col-lg-4 col-xs-6" *ngFor="let client of clients;index as i"> <!-- small box --> <div class="small-box bg-dashboard-box" > <div class="inner"> <div class="text-black"> ...

New techniques for integrating jQuery with Angular 4

As I delve into learning Angular 4, my goal is to apply it to various projects. While I am still grasping the basics, I have encountered noticeable differences when compared to using jQuery for DOM manipulation. The transition to using Angular has presente ...

Can Typegoose classes be used as types?

Currently, I am working on integrating my client class with a typegoose model named Member. Although I know how to use it, I am facing difficulties in setting up the types and intellisense correctly. The class is exported as MemberClass, and I need assista ...

Encountering issues with ng serve or ng build functionality following an upgrade from Angular 11 to Angular 13

Attempting to upgrade Angular from version 11 to 13, here is my package.json. I was facing slow build times with Angular 11, so I decided to make the switch to version 13. However, after upgrading, I am unable to build or serve the application. { "n ...

Encountering an issue with importing from 'sockjs-client' in TypeScript

I am a beginner with Angular and TypeScript. To get started, I used npm to install the following package: npm install --save sockjs-client I attempted to import it in my chat.component.ts file like this: import * as SockJS from 'sockjs-client'; ...

What is the process for defining a timeout for an HttpHandler using Angular?

I'm currently implementing a max timeout for HTTP requests using Angular5 and here's my approach based on some research: @Injectable() export class TokenInterceptor implements HttpInterceptor { constructor(private tokenService:TokenService) {} ...