The component is rendering properly, however the router-outlet in Angular seems to be getting overlooked

I've set up a router-outlet in app.component.html, admin.component.html, and manage-users.component.html. However, I'm facing an issue where the router-outlet in manage-users.component.html is not showing anything when I navigate to http://localhost:4200/admin/users

The file structure is organized as follows:

/Admin (Module)
     admin.component.html (includes router-outlet)
     admin.component.ts
     admin.module.ts
     admin-routing.module.ts
     /Manage-Users (Module within Admin module)
          manage-users.component.html (includes router-outlet)
          manage-users.component.ts
          manage-users.module.ts
          manage-users-routing.module.ts
          /User-List (component displayed in manage-users outlet)
          ...
          /User-Detail (component displayed in manage-users outlet)
          ...

There are 3 levels of routes defined:

    // App Routes (app-routing.module.ts)
    const appRoutes: Routes = [
      { path: 'admin', loadChildren: './admin/admin.module#AdminModule', canLoad: [AuthGuard] },
      { path: '',   redirectTo: '/login', pathMatch: 'full' },
      { path: '**', component: PageNotFoundComponent }
    ];

    // Admin Module Routes (admin-routing.module.ts)
    const adminRoutes: Routes = [
      {
        path: '',
        component: AdminComponent,
        children: [
          { path: 'users', component: ManageUsersComponent }
        ]
      }
    ];

    // Admin Module > Manage Users Module Routes (manage-users-routing.module.ts)
const manageUsersRoutes: Routes = [
  {
    path: '',
    component: ManageUsersComponent,
    children: [
      {path: '', component: UserListComponent},
      {path: 'edit/:id', component: UserDetailComponent}
    ]
  }
];

Here's how my ManageUsers.module is structured:

@NgModule({
  declarations: [
    ManageUsersComponent,
    UserListComponent,
    UserDetailComponent
  ],
  imports: [
    CommonModule,
    SharedModule,
    ManageUsersRoutingModule
  ],
  providers: [
    UserService
  ]
})
export class ManageUsersModule { }

And this is the setup of my Admin.module:

@NgModule({
  declarations: [
    AdminComponent
  ],
  imports: [
    CommonModule,
    AdminRoutingModule,
    ManageUsersModule
  ]
})
export class AdminModule { }

Answer №1

To properly load components inside the router outlet of your user routes, you must use the children attribute.

Your route setup is incorrect because you are importing the routing Module for user routes outside of UserModule, which appends them to the existing routes inside Admin Module.

The situation can be better understood with this code snippet:

const adminRoutes: Routes = [
  {
    path: '',
    component: AdminComponent,
    children: [
      {
        path: '',
        children:
        [
           { path: 'users', component: ManageUsersComponent }
        ]
      }
    ]
  },
    ...manageTenantRoutes   // additional routes here
];

One solution is to implement lazy loading like in your Admin module.

If you choose to keep it as is, you need to specify where these routes should be added.

Check out the updated stackblitz example here:

https://stackblitz.com/edit/angular-ryjdxc

Ensure you make the following changes:

import { NgModule } from '@angular/core';
import { Routes, RouterModule } from '@angular/router';
import { AdminComponent } from './admin.component';
import { ManageUsersComponent } from './manage-users/manage-users.component';

import { manageTenantRoutes } from './manage-users/manage-users-routing.module'
const adminRoutes: Routes = [
  {
    path: '',
    component: AdminComponent,
    children: [
      {
        path: '',
        children:
        [
          ...manageTenantRoutes
        ]
      }
    ]
  }
];

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

Update your routes accordingly:

export const manageTenantRoutes: Routes = [
  {
    path: 'users',
    component: ManageUsersComponent,
    children: [
      {path: '', component: UserListComponent},
      {path: 'edit/:id', component: UserDetailComponent}
    ]
  }
];

I hope this clarifies the issue!

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

Incorporate matTooltip dynamically into text for targeted keywords

I'm currently tackling a challenge in my personal Angular project that utilizes Angular Material. I'm struggling to find a solution for the following issue: For instance, I have a lengthy text passage like this: When you take the Dodge action, ...

What's the trick to inserting a "dot" beneath a DatePicker?

Could someone assist me in adding a small "dot" below certain dates on MUIX DatePicker, similar to the example shown here? Thank you. ...

Determine the type of a nested class within TypeScript

Utilizing nested classes in TypeScript is achieved through the following code snippet: class Parent { private secret = 'this is secret' static Child = class { public readSecret(parent: Parent) { return parent.secret } } } ...

Bringing in the RangeValue type from Ant Design package

Currently working on updating the DatePicker component in Ant Design to use date-fns instead of Moment.js based on the provided documentation, which appears to be functioning correctly. The suggested steps include: import dateFnsGenerateConfig from ' ...

Ensure that an input control consistently displays the value it is linked to

Here is the code snippet of the component: @Component({ template: ` <form> <label>Enter your name:</label> <input #name name="name" [ngModel]="firstName" (change)="onNameChange(name.value)"> </form> <p>Y ...

Angular application encountering a 401 unauthorized error when attempting to connect to TFS API

Having trouble with an API get request in my initial Angular project. I'm attempting to retrieve all projects from a server using the correct URL (which works in Postman and my browser), as well as a token generated from my TFS account. Despite this, ...

What could be the reason for the absence of {{ list.title }} on display

I'm currently learning how to develop my first MEAN stack app by following a tutorial on YouTube. However, I've encountered an issue where the title of the list is not displaying correctly. I'm using Insomnia to create the lists. Here's ...

Exploring the use of TypeScript and Webpack to read non-JavaScript files in Node.js

I'm working on a backend NodeJS setup written in TypeScript that is using webpack for compilation. However, I encountered an error when trying to read a text file even though I confirmed that the file source/test.txt is being copied to the build fold ...

Encountering an issue with subscribing wait times

Whenever a button is clicked within my application, I have a requirement to upload specific items to the API before proceeding with the next functionality. I've experimented with various approaches to ensure that the code waits for execution, but it ...

Positioning CSS for a Responsive Button

Currently, I am working on making my modal responsive. However, I am encountering an issue with the positioning of the "SAVE" button. The button is not staying in the desired position but instead disappears. Below is the snippet of my HTML and CSS: .dele ...

Update the Angular material table with the set filtered results

Currently, I have a functioning Angular Material table with search capabilities. However, I am encountering an issue. The problem lies in the fact that when I navigate from 'route A' to 'route B' and pass a value to the search box in t ...

Unleashing the full potential of Azure DevOps custom Tasks in VS Code and TypeScript: A guide to configuring input variables

Scenario: I have developed a custom build task for Azure DevOps. This task requires an input parameter, param1 The task is created using VS Code (v1.30.1) and TypeScript (tsc --version state: v3.2.2) Issue During debugging of the task, I am unable to pr ...

Ensuring that the designated key of an object is an extension of Array

What is the best way to ensure that the specified keyof a type extends an array? Consider the following type example: type TestType = { arrayKey: Array<number>, notArrayKey: number } There is also a function that accesses the specified key ...

What is the process for implementing angular-material's pre-defined theme variables in component styling?

I was trying to create a more dynamic background-color for my .active class within my mat-list-item Here is the HTML: <mat-list-item *ngFor="let page of pages" matRipple routerLinkActive="active" > < ...

What makes an Angular project different from conventional JavaScript projects that prevents it from running in a browser like any other?

When attempting to run index.html from the dist folder in the browser, I encountered issues. This is different from an AngularJS application where simply importing the script file into index.html allows the application to work seamlessly. Why is it not po ...

Is it possible to define a variable within an array declaration?

I am trying to construct an array for the week, with each element being an instance of my "work hours" class. However, when attempting to define them within the array directly, I encounter an error. Upon inspecting the JS file, I noticed that the array is ...

"Retrieving an element from an array may result in a value of

While going through an array of objects in my Angular component class, I faced a strange issue where the properties kept showing up as undefined. The function responsible for this behavior looks like this: upload(): void { const { fileHandles, related ...

Display Bootstrap Modal Box automatically when Angular 8 App Loads

I am facing an issue with launching a modal dialog on page load in my Angular 8/Visual Studio project. While it works perfectly fine with the click of a button, I am unable to make it load automatically on page load. I have tried using *ngIf but it doesn&a ...

How can we assign dynamic unique IDs to HTML elements within a for..of loop in TypeScript?

Within my array, I am using a for..of TypeScript loop to dynamically add identical HTML elements. Each element needs to have a unique ID. Is it feasible to achieve this directly within the for..of Typescript loop? The code snippet is as follows: for (le ...

How to Retrieve Observable<Record<string, boolean>> Value with the Help of AsyncPipe

My service retrieves permissions for the currently logged-in user as a record. The necessary observable is declared in my TypeScript file as a member variable: permissionsRecord$: Observable<Record<string, boolean>>; When I call the service, ...