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

Encountered an issue when deploying on CF: "ERROR The serve command must be executed within an Angular project, however a project definition could not be located."

I need to set up my Angular Application on Cloud-Foundry. Here is the package.json file currently located in the dist folder: { "name": "showroom-app", "version": "0.0.0", "engines": { "node" ...

Error: Unable to locate binding.gyp file

I'm currently in the process of setting up the required modules for my web application. As I execute $npm install, an error message pops up: john@mylaptop frontend % npm install > <a href="/cdn-cgi/l/email-protection" class="__cf_ ...

Is there a way for me to change the value and placeholder attributes on the Clerk's SignIn component?

Within Clerk's documentation, there is guidance on accessing the input field using the appearance prop as demonstrated below: <SignIn appearance={{ elements: { formFieldInput: 'bg-zinc-300/30' } }}/& ...

Tips for removing the Y-Axis entirely from a CanavasJS chart

I'm working with a canvasJS chart and my goal is to completely hide the Y-Axis. I've managed to remove the Y-Axis line and title in this JSFiddle example. I came across these properties to hide the Y-Axis title and line, but I'm struggling t ...

Angular 2: Capturing scroll events from the parent element within a Directive

One of the challenges I encountered is with a directive called [appInvalidField] that functions like a custom tooltip for validation purposes. To ensure it appears above everything else within dialogs, I attach it to the body and position it near the relev ...

The search button in the ngx-pagination StartDate and EndDate Search Filter is unresponsive

Working with Angular-14 and ASP.Net Core-6 Web API to consume an endpoint and handle JSON responses. An example of the endpoint URL without parameters: https://localhost/MyApp/api/v1/all-merchants And when parameters are included: https://localhost/MyApp ...

Ensuring accurate properties are sent to popup notifications

As a newcomer to a React & ASP.NET project, I am facing a challenge in displaying upload status notifications. The task may seem simple, but I need help in figuring out how to create popup notifications that indicate which files have been successfully uplo ...

Is it possible to modify the host header within an Angular app?

I'm experiencing a vulnerability issue and to resolve it, I need to utilize SERVER_NAME instead of the Host header. Is it possible to accomplish this using Angular? ...

Tips for managing Angular modules post-splitting and publishing on npm

Currently, I am in the process of developing an Angular application. To ensure reusability and prevent duplication of components and services in another application, I have divided the app into modules and deployed them on a private npm and git server. No ...

The argument represented by 'T' does not align with the parameter represented by 'number' and therefore cannot be assigned

I am confused as to why, in my situation, <T> is considered a number but cannot be assigned to a parameter of type number. Changing the type of n to either number or any resolves the issue. Error: Code: const dropFoo = <T>(arr: T[], n: T): T ...

Finding a solution to the dilemma of which comes first, the chicken or the egg, when it comes to using `tsc

My folder structure consists of: dist/ src/ In the src directory, I have my .ts files and in dist, I keep my .js files. (My tsconfig.json file specifies "outDir":"dist" and includes 'src'). Please note that 'dist' is listed in my git ...

Different methods for organizing an array of strings based on eslint/prettier

I possess an assortment of keys that I desire to sort in alphabetical order whenever I execute eslint --fix/prettier. My inference is that such a feature does not exist by default due to its potential impact on the code's behavior. Therefore, my quer ...

Incorporate a new element into a dynamic, responsive form using Angular

I’m struggling to create a nested reactive form in Angular 5. I can add items at one level successfully, but I’m having trouble adding items at the second level. ngOnInit() { this.orderForm = this.formBuilder.group({ customerName: '', e ...

Access to Angular component generation was denied due to EACCES permissions issue

Every time I attempt to create a new component for my Angular application, I immediately encounter a permission denied error without any clear explanation. The command that triggers this issue is ng g component heroes I have attempted to apply chmod -R 7 ...

How can ng-content be used to adjust the displayed content in Angular?

I am working with two components: hostComponent and textComponent. My goal is to project content inside textContent and make modifications based on other input properties. <app-text-component characterCount='5'> <span> Hello World ...

Incorporate an HTML span element with an onclick function bound in Angular framework

Is there a way to incorporate different icons by adding a span based on a flag, with an onclick event that triggers an internal function defined in the component ts? testfunc(){ console.log("it works") } flagToIcon(flag: boolean) { switch ( ...

What is the significance of incorporating 'Actions' as data within the Redux framework?

According to Redux documentation, creating actions and action creators is necessary. Here's an example: function addTodo(filter) { return { type: SET_VISIBILITY_FILTER, filter } } Next step is to write reducers, like this: function t ...

The Freemode feature in SwiperJS is not functioning properly when used with React TypeScript

Having a slight issue with SwiperJS. Using version 10.1.0 and the following code : import { Swiper, SwiperSlide } from "swiper/react"; import "swiper/css"; export default function Discover() { return ( <> ...

Even after ensuring the proper type checking, I am still receiving the error message "Property 'message' does not exist on type 'object'"

I have the following code snippet: try { // api call } catch (error) { if (typeof error === 'object' && error !== null && 'message' in error) { if (typeof error.message === 'string') { if (error.me ...

Delete an item from an array when a dropdown selection is made

When dealing with Angular 8, I encountered a logic issue. There are two drop-down menus: First Drop-down The options in the first menu are populated from an array of objects Example Code, ts: {rs_id: "a5f100d5-bc88-4456-b507-1161575f8819", ...