Navigate the nested route of a child page starting from the main Root component

I am facing an issue with enabling nesting routes on the BarcodeScannerComponent component. I have attempted the following method, but it does not seem to work. The main challenge lies in accessing the nested route within the root component, which is app.component.ts. This is because my navigation button resides in the HeaderComponent. Any suggestions or clues on how to resolve this?

Although I can accomplish the task without using nesting routes, it leads to the dashboard component being destroyed when the user navigates to the barcode page. To prevent this from happening, I have opted for the Nesting route pattern.

app.component.ts

<div>

    <app-header> </app-header>

    <main>
        <router-outlet> </router-outlet>
    </main>
    
</div>

header.componet.html

<p-menubar>

   <i (click)="goToBarcodeScannerPage()"></i>

</p-menubar>

header.componet.ts

goToBarcodeScannerPage(): void {
   this.router.navigateByUrl('barcode-scanner'); // This is not working
 }

dasboard.componet.html

// removed other HTML

<router-outlet></router-outlet>

dashboard-routing.module.ts

const routes: Routes = [
  {
    path: '',
    component: DashboardComponent,
    children: [
      {
        path: 'barcode-scanner',
        component: BarcodeScannerComponent
      }
    ]
  },
];

app-routing.module.ts

const routes: Routes = [
  
  {
    path: 'dashboard',
    loadChildren: () => import('./modules/dashboard/dashboard.module').then(m => m.DashboardModule),
    ...canActivate(redirectUnauthorizedToLogin)
  },
  
];

Answer №1

goToBarcodeScannerPage(): void {
  this.router.navigateByUrl('/dashboard/barcode-scanner');
}

Ensure to use the absolute path by adding a slash (/) in front of the URL.

In your code, the dashboard component includes a <router-outlet> where the barcode component will be displayed. To prevent the dashboard component's HTML from showing when displaying the barcode component's content, it is important to create a new component that encapsulates the dashboard component's HTML and set up a route for it as shown below.

// Create a new component called `DashboardMainComponent` that contains the HTML content of `Dashboard`

In dashboard-routing.module.ts

const routes: Routes = [
  {
    path: '',
    component: DashboardComponent,
    children: [
      {
        path: '',
        component: DashboardMainComponent
      },
      {
        path: 'barcode-scanner',
        component: BarcodeScannerComponent
      },
      {
        path: '**',
        redirectTo: ''
      }
    ]
  },
];

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 Bootstrap Datepicker provides us with a date object, but I need it in the Date format

My desired date format is "Wed Aug 07 2019 16:42:07 GMT+0530 (India Standard Time)", but instead I am receiving { year: 1789, month: 7, day: 14 } from ngbDatepicker. Any assistance on resolving this issue would be greatly appreciated. ...

What is the process for generating an alert box with protractor?

While conducting tests, I am attempting to trigger an alert pop-up box when transitioning my environment from testing to production while running scripts in Protractor. Can someone assist me with this? ...

"Encountering a Bug in Angular 2 Related to Chart.js

I am currently in the process of developing a Twitter-style application using a Typescript/Angular2 front-end framework and a Node.js back-end. The foundation of my project is derived from Levi Botelho's Angular 2 Projects video tutorial, although I h ...

"Although the NextJS client-side data is present, it seems to be struggling to

I am facing an issue while trying to display fetched data on a blog page. Although the data is successfully retrieved client-side and I can view it using console.log(), it does not appear on the page and the elements remain empty. I am completely puzzled. ...

What is the functionality of the node class within a doubly linked list?

Within the Node class, the next property can only be assigned a value of type Node or null. class Node { value: any; next: Node | null; prev: Node | null; constructor(value: any) { this.value = value; this.next = null; this.prev = null ...

What could be the reason for the GET request not functioning properly in Node.js?

const express = require('express'); const mongoose = require ("mongoose"); const app = express(); const Student = require('../models/students'); require('dotenv').config(); const PORT = process.env.PORT || 3000; const co ...

Unable to transfer data through Ionic popover

I've encountered an issue when trying to pass data to my popover component, as the data doesn't seem to be sent successfully. Code HTML <div class="message" id="conversation" *ngFor="let message of messages.notes"> <ion-row class= ...

When we mention TypeScript and CDK, we are actually talking about the very foundation

As I was working on my current Stack constructor, I came across the Stack.formatArn() method. I started to wonder about the difference between using this.formatArn() and cdk.Stack.of(this).formatArn(). After all, shouldn't "this" refer to the stack it ...

Issue encountered while executing jest tests - unable to read runtime.json file

I've written multiple unit tests, and they all seem to pass except for one specific spec file that is causing the following error: Test suite failed to run The configuration file /Users/dfaizulaev/Documents/projectname/config/runtime.json cannot be r ...

What is the best way to remove all attributes from one interface when comparing to another?

Consider the following two interfaces: interface A { a: number; b: string; } interface B { b: string; } I am interested in creating a new type that includes all the keys from interface A, but excludes any keys that are also present in interface B. ...

Unveiling the Ultimate Method to Package Angular 2 Application using SystemJS and SystemJS-Builder

I'm currently in the process of developing an application and I am faced with a challenge of optimizing the performance of Angular 2 by improving the loading speed of all the scripts. However, I have encountered an error that is hindering my progress: ...

Trouble authenticating user through express-session with Typescript

I have developed a small app for registration and login, but I am encountering issues with using session-express to maintain the session. Check out server.ts below where I establish session, cors, etc... import express, { json } from "express"; ...

Angular and Bootstrap combined to create a versatile navigation bar with a collapsible sidebar feature

Currently, I am in the process of developing a progressive web app using Angular and Bootstrap. One of the main challenges I am facing is implementing a navbar that not only looks great on desktop but also on mobile devices. So far, I am satisfied with my ...

What is the best way to remove any objects in this array that have empty strings as values?

I have been developing a form using Angular 14. The form consists of a primary section, which includes the user's necessary information, and a secondary section where users can input additional data (an array of residences displayed in a table) befor ...

Issue encountered while attempting to run an Angular project using the CLI: "Module not found - Unable to resolve 'AngularProjectPath' in 'AngularProjectPath'"

Just like the title suggests, I'm facing an issue with compiling my angular project. It seems to be having trouble resolving my working directory: Error: Module not found: Error: Can't resolve 'D:\Proyectos\Yesoft\newschool& ...

An error occurred while uploading a file in the StaticInjectorError of the AppModule related to the HttpHandler and Injector

Hey there! I'm currently working on a project using Angular 9 and Angular Material. I'm trying to implement the mat-file-upload feature, but when I run the app, I keep getting this error message: "StaticInjectorError(AppModule)[HttpHandler -> ...

React-table fails to show newly updated data

I am facing an issue with my react-table where real-time notifications received from an event-source are not being reflected in the table after data refresh. https://i.stack.imgur.com/q4vLL.png The first screenshot shows the initial data retrieval from th ...

In Typescript, is there a specific type for images encoded in base64 format?

As a newbie to Typescript, I am diligently typing everything precisely as part of my learning journey. A property called lqip (low quality image placeholder) is being pulled from a CMS and should be a base64 encoded image. It's clearly a string, but ...

Utilizing React Typescript to dynamically render a duo of components

On a single page, I want to display two components simultaneously. There is a bottom navbar that, when clicked on, for example the profile icon, should render the profile page. However, I would like to change the color of the icon based on which component ...

Tips for updating the display after making an angular $http request using rxjs Observables

I have a project where I am utilizing angular's $http service to fetch data from a remote endpoint. I am keen on incorporating rxjs Observables, hence the call in my service is structured as follows: userInfo() : Rx.Observable<IUserInfo> { ...