Angular relative routes are failing to function

I am currently working on implementing a feature module in my project and following the documentation provided.

My crisis-routing.module file looks like this:

import { NgModule } from '@angular/core';
import { Routes, RouterModule } from '@angular/router';
import { CrisisCenterComponent } from './crisis-center.component';
import { CrisisListComponent } from './crisis-list/crisis-list.component';
import { CrisisDetailComponent } from './crisis-detail/crisis-detail.component';
import { CrisisCenterHomeComponent } from './crisis-center-home/crisis-center-home.component';

const routes: Routes = [
  {
    path: 'crisis-center',
    component: CrisisCenterComponent,
    children: [
      {
        path: '',
        component: CrisisListComponent,
        children: [
          {
            path: ':id',
            component: CrisisDetailComponent
          },
          {
            path: '',
            component: CrisisCenterHomeComponent
          }
        ]
      }
    ]
  }
];

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

While I thought I would be able to access the id with this code snippet:

 [routerLink]="['./crisis-center/..', crisis.id]"

I could use some assistance, can anyone help me?

Answer №1

In my experience, I have discovered that Angular has a recommended method for organizing parameters with child pages. The route declarations would look something like this:

children: [
      {
        path: ':id',
        component: CrisisListComponent,
        children: [
          {
            path: 'crisis-home',
            component: CrisisCenterHomeComponent
          }
        ]
      }
    ]

To access the routes, you can use the following syntax:

[routerLink]="['./crisis-center/..', crisis.id, 'crisis-home']"

This will generate the URL:

localhost/crisis-center/1/crisis-home

Answer №2

While I have successfully implemented some functionality, I am facing challenges when it comes to working with nested children.

My updated routes are as follows:

import { NgModule } from '@angular/core';
import { Routes, RouterModule } from '@angular/router';
import { CrisisCenterComponent } from './crisis-center.component';
import { CrisisListComponent } from './crisis-list/crisis-list.component';
import { CrisisDetailComponent } from './crisis-detail/crisis-detail.component';
import { CrisisCenterHomeComponent } from './crisis-center-home/crisis-center-home.component';
import { CrisisDetailedComponent } from './crisis-detailed/crisis-detailed.component';

const crisisRoutes: Routes = [
  {
    path: 'crisis-center',
    component: CrisisCenterComponent,
    children: [
      {
        path: '',
        component: CrisisListComponent,
        // children: [
        //   { path: '', component: CrisisCenterHomeComponent },
        //   { path: 'crisis/:id', component: CrisisDetailedComponent },
        // ]
      },
      { path: ':id', component: CrisisDetailedComponent },
      { path: 'home', component: CrisisCenterHomeComponent },
    ]
  }
];

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

I'm puzzled as to why only one nested route is being accepted; it's possible that I am not accessing the routes correctly.

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

Synchronously updating the state in Angular using NgRx

Currently, I have implemented NgRx-Effects along with an action to fetch data from a service and update my initial state. The state property name, let's call it 'schoolData', is functioning smoothly. However, I now require to modify the sam ...

Analyzing arrays and object key/value pairs based on a specific value in javascript

I want to create a new object with key/value pairs. The new object should include values from an existing key/value object as well as unique values from an array. Here is the array: [{ name: "Computer", name: "Car", name: "House&q ...

Tabs justified are not constrained by width

Utilizing Bootstrap 3, I aim to have the Tabs align perfectly with the full content. The use of nav-tabs can be observed in the code below. It may seem a bit unusual due to my implementation of Angular 4 and the code being copied from Dev Tools. <ul cl ...

Tips for hiding a <div> styled as a tooltip in an Angular application when the user clicks elsewhere

I am currently developing an Angular 7 application. One of the features I have implemented is an interactive icon that reveals an absolutely positioned tooltip component when clicked on by the user. However, I am faced with the challenge of making the too ...

Performing an action within the Redux RTK API Slice

Is it feasible to trigger an action from a different reducer within the API Slice of Redux RTK? Let's say I have this scenario: getSomething: builder.query<SomeProps, void>({ query: () => ({ url: "...", ...

What is the best way to invoke a function in a functional React component from a different functional React component?

I need to access the showDrawer method of one functional component in another, which acts as a wrapper. What are some best practices for achieving this? Any suggestions or insights would be appreciated! const TopSide = () => { const [visible, se ...

Getting event properties in a React component using the rest operator: A comprehensive guide

Can someone please assist me? I am new to TypeScript and struggling with how to use event props in my component. I have defined two props and need all my events as rest props. I encountered an error when trying to use my component with onClick event. The ...

Different Options to Explore Instead of Using @apollo/federation Library

We are currently developing typescript microservices with REST APIs and are exploring the possibility of integrating GraphQL into each microservice, along with implementing an API Gateway at the top level to combine everything into a single API. Although ...

Testing Angular application with a currency pipe results in an error stating "currency

Utilizing the built-in angular currency pipe in my components works perfectly fine. However, when attempting to unit test my component, an error occurs: https://i.stack.imgur.com/J18JL.png I am using Angular 10 with Ivy and have imported the CommonModule, ...

The functionality of router.navigate in Angular 5 seems to be malfunctioning

I am struggling with redirecting a user to another page based on a certain condition. Here is an example of my login component: ngOnInit() { console.log(">>> router", this.router) console.log(">>> activatedRoute", this.activate ...

"Unindexing data in Angular: A step-by-step guide

Can someone help me figure out how to delete an item by index in Angular? I have a parameter and a remove button, but when I tried putting my parameter inside the remove button it didn't work. How can I fix this? deleteRowFiles(rowIndex: number){ th ...

Creating a unified observable by merging various iterations in RXJS

For a while now, I've been grappling with the challenge of combining multiple asynchronous calls. Every time I think I'm close to figuring it out, I hit a roadblock at a foreach loop that I just can't seem to crack. Currently, my approach i ...

Oops! An issue occurred during the `ng build` command, indicating a ReferenceError with the message "Buffer is not defined

I'm facing an issue while trying to utilize Buffer in my angular component ts for encoding the Authorization string. Even after attempting npm i @types/node and adding "node" to types field in tsconfig.json, it still doesn't compile with ng buil ...

The addition operator is not compatible with the given types

Hello, I am currently working on integrating PayPal into an Angular 5 project. The code snippet below shows how I render PayPal buttons using a function: ngAfterViewChecked(): void { if (!this.addScript) { this.addPaypalScript().then(() => { ...

Parent and router-outlet in Angular 4 fashion

Suppose we are presented with the following code: <div class="container"> <div class="row"> <div class="col-sm-4 col-md-3"> <app-sidebar></app-sidebar> </div> <div class="c ...

Display the spinner until the data is successfully updated in the DOM using Angular

Dealing with a large dataset displayed in a table (5000 rows), I am attempting to filter the data using column filters. When applying the filter, I have included a spinner to indicate that the filtering operation is in progress. However, due to the quick ...

The Angular 4 component's view does not reflect the updates made to the array

Recently, I started exploring Angular 4 and I must say it's an impressive framework developed by Google. I encountered a problem where my model was being updated after calling a REST API in a component, but the view wasn't reflecting these chang ...

Misunderstanding the concept of always being right

Here is a code snippet that raises an error in TypeScript: class Status { constructor(public content: string){} } class Visitor { private status: Status | undefined = undefined; visit(tree: Tree) { if (tree.value > 7) { this.status = new ...

Navigating Routes with Router in Angular 7: A Step-by-Step Guide

Within my sidebar navigation component, the sidebar.component.html file is structured as follows: <nav class="navbar navbar-expand-lg navbar-dark bg-primary fixed-top" id="sideNav"> <a class="navbar-brand" href="#page-top"> <span cl ...

Difficulty in converting class-based JS useState array to TypeScript Hook

Recently, I've delved into the world of React / TypeScript and have taken on a personal project to enhance my skills. As part of this project, I am in the process of transitioning some class-based code into hooks. Here is the (JS) class-based code th ...