Designated router outlet showing empty content

I'm currently tackling a project in Angular where I find myself in need of two router outlets. The primary outlet is responsible for loading main pages and the dashboard page, while the secondary outlet, which I've dubbed "mainview", loads the component selected in the sidebar of the dashboard page (I'm currently only testing this with URLs).

Below is the routing module that I've put together:

import { OrdersComponent } from './pages/orders/orders.component';
import { DashboardComponent } from './pages/dashboard/dashboard.component';
import { AuthentificationComponent } from './pages/authentification/authentification.component';
import { IndexComponent } from './pages/index/index.component';
import { NgModule } from '@angular/core';
import { RouterModule, Routes } from '@angular/router';

const routes: Routes = [
  {path: '', component: IndexComponent, pathMatch: 'full'},


  {path: 'login', component: AuthentificationComponent},
  {path: 'dashboard', component: DashboardComponent},
  {path: 'orders', component: OrdersComponent, outlet: 'mainview'},

  {path: '**', redirectTo: '', pathMatch: 'full'},
];

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

And here's the dashboard page that loads content into the secondary outlet:

<mat-drawer-container class="example-container">
    <mat-drawer mode="side" opened>
        <app-sidebar></app-sidebar>
    </mat-drawer>
    <mat-drawer-content>
        <router-outlet name="mainview"></router-outlet>
    </mat-drawer-content>
</mat-drawer-container>  

I attempted to load the orders component into the mainview using the following URL:

http://localhost:4200/dashboard(mainview:orders)

Here's how the dashboard page appears, with the white space waiting for the "mainview" outlet content to display: https://i.sstatic.net/IAssX.png

However, the area remains blank. I might be missing something obvious as this is my first time working with named router outlets. Please advise on how I can ensure that the secondary outlet "mainview" displays the orders component. Thank you!

Answer №1

When you are incorporating the OrdersComponent within the DashboardComponent, it is essential to load the DashboardComponent first, followed by the OrdersComponent. The setup provided will only function correctly if both the dashboard outlet and mainview outlet are located in your top-level component. To handle nested router-outlets, utilize the children property.

const routes: Routes = [
  {path: '', component: IndexComponent, pathMatch: 'full'},
  {
    path: 'dashboard',
    component: DashboardComponent,
    children: [
      {
        path: 'orders',
        component: OrdersComponent,
        outlet: 'mainview',
      },
    ],
  },
  {path: 'orders', component: OrdersComponent, outlet: 'mainview'},
  {path: '**', redirectTo: '', pathMatch: 'full'},
];

To navigate through this structure, use:

http://localhost:4200/dashboard/(mainview:orders)

In this scenario, specifying the outlet name is redundant since it's the sole router-outlet within the dashboard component.

Answer №2

For the functionality to work properly, you must use the routerLink directive on the link within the IndexComponent that you want to click in order to display the orders.

<a [routerLink]="[{ outlets: { mainview: ['orders'] } }]"> orders </a>

If you wish for the orders to be shown immediately upon navigating to the dashboard, you can add the following route to the routes array:

    {path: 'dashboard', component: OrdersComponent, outlet:'mainview'}

This will position the OrdersCompont on the right side of the IndexComponet.

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

Angular routing unit testing: Breaking down routing testing into individual route testing sequences

Currently, I am in the process of testing the routing functionality of my Angular application: Below is the file where I have declared the routes for my app: import { NgModule } from '@angular/core'; import { RouterModule, Routes } from '@ ...

Angular 2: Creating a Jasmine test case for accessing the query parameters in activateRoute.snapshot

Currently, I am extracting values from the URL using ActiveRoute in my Angular project. http://localhost:4200/project-test/#/add?orderId=156&customerNumber=431 In order to retrieve these values, I have implemented a separate component with an ngOnIni ...

Check for the presence of an event listener before adding it in Angular 5 using TypeScript

Currently, I am involved in developing a hybrid application using Angular. The form consists of components from both Angular 6 and AngularJS. This particular project is designed as a single-page application with data spread across ten different tabs. To en ...

Configuring Angular routes based on service method invocation

I have my routes configured in @NgModule. I also have a service that determines which parts of the application to display based on specific conditions. I need to call this service and adjust the routes according to its output. Issue: The route configurati ...

Maximizing Azure Web App capabilities to host multiple applications

I have developed an app using ASP .NET Core MVC + Angular and now I need to deploy it for three separate customers. Each customer currently has their own database. Is it feasible to create multiple sites within a single Azure web app (such as mysite.com/ ...

Issue with Datatables not loading on page reload within an Angular 7 application

Incorporating jQuery.dataTables into my Angular 7 project was a success. I installed all the necessary node modules, configured them accordingly, and added the required files to the angular.json file. Everything functioned perfectly after the initial launc ...

When using Material-UI with TypeScript, an error is thrown stating that global is not defined

After running the following commands: npm install --save react react-dom @material-ui/core npm install --save-dev webpack webpack-cli typescript ts-loader @types/react @types/react-dom I transpiled main.tsx: import * as React from "react"; import * as R ...

Sign up to receive updates on PageSize and PageIndex settings for MatTable in Angular Material

Currently, I am incorporating an angular material Table and paginator into my project. The challenge I am facing is how to send the pageSize and pageIndex values to the backend in order to handle paging on that side. Our application deals with a large am ...

Exploring Angular: Uncovering the data within a response object

Here is the information provided: I attempted the following method => this.result = res?.permissions?.action_findings.store; Could you please provide a more suitable response? ...

Combining Repetitive Elements in an Array

Trying to combine an array of products with the same order_id while also including all objects from a second products array. Below are some sample orders: const orders = [ { "order_details": { }, "order_id": "1", ...

Errors encountered while trying to install the ngx-admin template through npm

After cloning the ngx-admin angular template from the git repository located at https://github.com/akveo/ngx-admin, I encountered a series of errors when trying to install the node modules and run the project: npm ERR! code 1 npm ERR! path C:\Users&bs ...

React animation failing to render underline animation

After tinkering with the underline animation while scrolling down on Codepen using Javascript, I successfully implemented it. You can check out the working version on Codepen. This animation utilizes Intersection Observer and a generated svg for the underl ...

After downloading the quickstart (angularjs2) from angular.io, I encountered an error when trying to start npm

[email protected] typing start G:\quickstart\quickstart-master tsc && concurrently "tsc -w" "lite-server" [1] 'lite-server' is not recognized as a valid command, [1] executable program or batch file. [1] lite-serve ...

Configuration files and dynamic link libraries included in the Electron software package

I'm trying to figure out how to package dlls and configuration files using electron-builder or electron-packager. Despite searching the documentation for both tools, I haven't found an example of where to place these files when packaging the appl ...

Retrieve information for the designated page exclusively

When retrieving data from the backend using a service, I encounter an issue where the system may slow down if 2000 records are returned in one request. To address this, I would like to display only 10 records per page and fetch the next 10 records with eac ...

Setting up a ts-node project with ECMAScript modules. Issue: Unrecognized file extension ".ts"

I am currently in the process of setting up a minimalistic repository that incorporates ts-node and ESM for a project. Despite the existence of previous inquiries on this topic, I have encountered numerous outdated responses. Even recent answers appear to ...

Can the GitHub URL be utilized for installing TypeScript npm dependencies?

When working with an npm library written in TypeScript, the usual process involves writing the source code in TypeScript, pushing it to GitHub, then converting it to JavaScript and pushing the resulting JavaScript code to the npm repository. When adding ...

How to create a karma-jasmine test case in Angular 2 to trigger a modal opening when a link is clicked

I need to verify that when a link is clicked, a modal should open. In my spec.ts file, I have written the following test: describe('NavbarComponent', () => { let comp: NavbarComponent; let fixture: ComponentFixture<NavbarComponent& ...

The PDF can be accessed from the console but the network tab is not visible

import { axiosInstances } from "./axiosInstanc" interface upladPdfPayload { name: string; pdf: File } export const uploadPdfApi = async (payload: upladPdfPayload) => { try { console.log(payload); const res = await a ...

Evaluating an Angular 2.0.0 component by testing its functionality with an HTTP

I am currently working on an Angular 2.0.0 component that looks like this: import { Component, OnInit } from '@angular/core'; import { Http } from '@angular/http'; @Component({ selector: 'app-book-list', templateUrl: &ap ...