The culprit behind Angular 11 app error: Unidentified router outlet triggering 'No routes match' issue

In my Angular 11 application, I am utilizing a named router-outlet. The setup in my app.component.html file looks like this:

    <ng-container *ngIf="showGeneric">
        <router-outlet name="general">
        </router-outlet>
    </ng-container>

    <ng-container *ngIf="!showGeneric">
        <router-outlet name="non-general">
        </router-outlet>
    </ng-container>

The initial value of showGeneric is set to true in the app.component.ts file.

Furthermore, the routing configuration in my app-routing.module.ts file is defined as follows:

import { NgModule } from '@angular/core';
import { Routes, RouterModule } from '@angular/router';
import { NonGenericComponent } from './non-generic/main-component/non-generic.component';
import { GenericComponent } from './generic/main-component/generic.component';

 const routes: Routes = [
 {
   path: '',
   redirectTo: 'generic',
   pathMatch: 'full'
 },
 { 
   path: 'generic', 
   component: GenericComponent,
   loadChildren: () => import('./generic/generic.module').then(m => m.GenericModule),
   outlet: 'general'  
 },
 { 
   path: 'non-generic', 
   component: NonGenericComponent,
   loadChildren: () => import('./non-generic/non-generic.module').then(m => 
   m.NonGenericModule),
   outlet: 'non-general'  
 },
 {
   path: '**',
   redirectTo: 'generic',
   pathMatch: 'full'
 }
]

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

However, when attempting to navigate to http://localhost:4200/generic in the local development environment, an error is encountered: Error: Uncaught (in promise): Error: Cannot match any routes. URL Segment: 'generic'. I am currently unable to identify the root cause of this issue. Any assistance in resolving this problem would be greatly appreciated.

Answer №1

In accordance with @mJehanno's statement, there is no requirement for a named router outlet in order to achieve the desired functionality.

You can eliminate the named router outlets and instead directly pass children routes to the router configuration to load this module.

const routes: Routes = [
  {
    path: '',
    redirectTo: 'generic',
    pathMatch: 'full'
  },
  {
    path: 'generic',
    component: GenericComponent,
    children: [
      {
        path: '',
        loadChildren: () =>
          import('./generic/generic.module').then(m => m.GenericModule)
      }
    ]
  },
  {
    path: 'non-generic',
    component: NonGenericComponent,
    children: [
      {
        path: '',
        loadChildren: () =>
          import('./non-generic/non-generic.module').then(
            m => m.NonGenericModule
          )
      }
    ]
  },
  {
    path: '**',
    redirectTo: 'generic',
    pathMatch: 'full'
  }
];

When it comes to the HTML:

<a [routerLink]='["generic"]'>Generic</a> |
<a [routerLink]='["non-generic"]'>Non Generic</a> | 

<router-outlet></router-outlet>

View Sample Demo (Remember to check the console for details)

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

Utilize Pipe for every instance of a variable in the Controller

In my controller, I have multiple requests (POST, GET etc.) where the path includes an id parameter that needs to be a number string. I want to validate this parameter once and have it apply to all instances. Here is the current code snippet: @Get(&apo ...

Error occurs in Azure Function Linux Nodejs when trying to use @azure/storage-blob as it cannot read the property 'startsWith' of undefined

While testing my Azure Nodejs Linux function locally, I encountered this issue with the code snippet below: import { BlobServiceClient } from "@azure/storage-blob"; const connectionString = process.env[ "AZURE_STORAGE_CONNECTION_STRING&qu ...

Having trouble fetching values from an object in Angular? Receive an undefined result

Currently, I am attempting to extract values from a backend object. The initial demonstration works fine: export class Test { public StringValue: string | undefined; public BoolValue: boolean | undefined; public TestList: Array<User>; } Additi ...

Steps for displaying a new event on a fullCalendar

Utilizing fullCalendar to display a list of events in the following manner: this.appointments = [{ title: "Appointment 1", date: "2020-09-06", allDay: false }, { title: "Appointment 2", date: "2020 ...

Having trouble implementing ng2-charts in Angular 4 when using shared components

Using angular 4.0.0, angular-cli 1.2.1, and ng2-charts 1.6.0 has been a smooth experience for me so far. However, I encountered an issue when trying to incorporate ng2-charts into "shared" components. In my setup, I have a shared-components.module that is ...

Difficulty encountered when attempting to create a lambda function in TypeScript

I'm having trouble understanding the following lambda function definition in TypeScript. It could be a beginner question. type P = { id: string } type F = <T>(x: T) => string const f: F = (x: P) => x.id f({ id: 'abc' } However, ...

Why does the property of {{hero.name}} function properly in a <h> tag but not in an <img> tag?

Within this template, the code below functions correctly: <h3>{{hero.name}}</h3> It also works for: <a routerLink="/details/{{hero.id}}">{{hero.name}}</a> However, there seems to be an issue with the following image path ...

Which is better: Angular module for each page or component for each page?

We are getting started with Angular 8 and have a vision for a web application that includes: A navigation bar at the top with dropdown menu items leading to different UI pages. The main section will display each page, such as: - Sales section ...

What is the best method for setting a dash as the default value in a template?

My HTML code in Angular 8 includes the following: <span>{{post.category || '-'}}</span> If post.category is empty, a dash is shown as expected. However, I am facing two other situations: <span>{{post.createdAt || '-&apo ...

Binding two objects to a single event using Angular 2 syntax

Is there a way to connect two simple input fields to a single click event in Angular? One box for typing text and the other providing a timestamp from Date();. How can I show both values when clicking on the button? // The #date input field provides the ...

Angular: Extracting the default value of a FormControl within a Child Component

Is there a way to retrieve the initial value set in a FormControl for a custom component within its ngOnInit method? Suppose you have created a form control like this: testControl = new FormControl("abc123"); And then linked it to your custom component ...

Something went wrong in the prerender.ts file at line 7. The error message is indicating that it cannot locate the module './dist-prerender/main.bundle'

Encountering an error while compiling the Angular code for prerendering: ERROR in prerender.ts(7,62): error TS2307: Cannot find module './dist-prerender/main.bundle' npm ERR! code ELIFECYCLE npm ERR! errno 1 npm ERR! [email protected] b ...

Issue with React not displaying JSX when onClick Button is triggered

I've recently started learning React and I'm facing a problem that I can't seem to figure out. I have a basic button, and when it's clicked, I want to add another text or HTML element. While the console log statement is working fine, th ...

Exploring TypeScript integration with Google Adsense featuring a personalized user interface

After following a tutorial on implementing Google AdSense in my Angular App, I successfully integrated it. Here's what I did: In the index.html file: <!-- Global site tag (gtag.js) - Google Analytics --> <script> (function(i,s,o,g,r,a,m ...

Ensure all promises are resolved inside of for loops before moving on to the next

Within my angular 11 component, I have a process that iterates through elements on a page and converts them from HTML to canvas to images, which are then appended to a form. The problem I am encountering is that the promise always resolves after the ' ...

Is the Angular 7 router '**' wildcard feature failing to work properly when used in conjunction with lazy loaded modules and child routes?

I'm currently working on setting up a default route using the wildcard '**' with Angular's router. The intention is for this default route to load a lazy module and then handle its own routes internally. However, I have run into an issu ...

Setting dynamic values within the constructor of a TypeScript class to the object instance

Can you help me troubleshoot an issue in this simplified class code snippet? I'm attempting to dynamically assign values to this by looping over key values in the constructor, but it's not working as expected. Could this be a syntax problem or is ...

Modify the icon for the angular material datePicker

I was pondering the possibility of changing the icon displayed when using the datePicker feature in Angular Material. Here is the code snippet provided in the Angular Material documentation. <md-input-container> <input mdInput [mdDatepicker]=" ...

Sending an email using Angular is a straightforward process that involves utilizing the built

I need help figuring out how to code a function in Angular or TypeScript that will open Gmail when a specific email address is clicked. I've tried different methods but haven't been successful so far. ...

Unable to set textAlign column property in Inovua React Data Grid using typescript

I am currently facing an issue with centering the content of each grid cell in Inovua RDG. A frustrating TypeScript error keeps popping up: Type '{ name: string; header: string; textAlign: string; defaultFlex: number; defaultVisible?: undefined; }&apo ...