The Angular 2 router is unable to navigate to child auxiliary routes

I have been attempting to implement an auxiliary route for a modal window, but I am encountering routing errors and unable to resolve the issue. Here is how I have approached it...

app.component.html

<router-outlet></router-outlet>
<router-outlet name="modal"></router-outlet>

admin-routing.module.ts

...
const ROUTES: Routes = [
  {
    path: 'admin',
    component: AdminComponent,
    children: [
      {
        path: 'accounts',
        loadChildren: './accounts/accounts.module#AccountsModule'
      },{...}
    ]
  }
];
...

accounts-routing.module.ts

...
const ROUTES: Routes = [
  {
    path: '',
    children: [
      {
        path: '',
        component: AccountsIndexComponent
      },{
        path: 'new',
        component: AccountsNewComponent
      },{
        path: ':id',
        component: AccountsShowComponent
      },{
        path: ':id/edit',
        component: AccountsEditComponent,
        outlet: 'modal'
      }
    ]
  }
];
...

modal.service.ts

  ...
  constructor(
    private router:Router,
    private route:ActivatedRoute
  ) { }
  open(route:Array<any>) {
    this.router.navigate(
      [{outlets: {drawer: route}}], {relativeTo: this.route}
    )
  }
  ...

When I use

ModalService.open([account.id, 'edit'])
, the navigation attempts to go to /admin/accounts(modal:1/edit), which seems like the correct behavior. However, I receive an error indicating that it cannot match the 1/edit URL segment.

NavigationError(id: 2, url: '/admin/accounts(modal:1/edit)', error: Error: Cannot match any routes. URL Segment: '1/edit')

I have also attempted the following without success...

ModalService.open(['accounts', account.id, 'edit'])
ModalService.open(['admin','accounts', account.id, 'edit'])

Answer №1

It appears that a known issue in the router is causing child auxiliary routes to not load when the parent path is empty, as seen in accounts-routing.module.ts:

const ROUTES: Routes = [
  {
    path: '',                 // <--- cannot have an empty parent path...
    children: [
      {
         path: ':id/edit',
         component: AccountsEditComponent,
         outlet: 'modal'      // <--- ...with child aux outlet
      }
    ]
  }
]

To address this problem, we need to provide something for the path. For example, you can name your path 'accounts':

const ROUTES: Routes = [
  {
    path: 'accounts', 
    ...

from there, you can navigate to your aux route either through a routerLink:

<a [routerLink]="['accounts', {outlets: {'modal': ['1/edit']}}]">

or programmatically:

this.router.navigateByUrl('accounts/(modal:1/edit)')

Here is a working Plunkr created to troubleshoot and demonstrate successful loading of a child auxiliary route.

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

The class function in the exported typescript logs that "this" is not defined

I am currently facing an issue with my TypeScript class where I am setting class values in a constructor and referencing them using "this" in a class method. While the .ts file compiles without any warnings, when I import the compiled .js file into another ...

The matToolTip will not appear regardless of whether filtering/ngFor is implemented or not

Take a look at I came across this post about ngFor, and also found some insights on GitHub Interested in finding solutions for both standalone use and when using ngFor. ...

Encountered an issue while attempting to run npm install angular2 --save

Currently, I'm following a Tuts+ tutorial on Angular. You can watch the video here: where the instructor instructs to enter the following command: npm install angular2 --save Despite trying both the command and adding sudo in front of it, I encount ...

Double-tap bug with Image Picker in Typescript React Native (non-expo)

Well, here’s the situation - some good news and some bad news. First, the good news is that the code below is functioning smoothly. Now, the not-so-good news is that I find myself having to select the image twice before it actually shows up on the clie ...

Encountering issues with NPM installation and a failed build process while working with

As someone new to working with Angular, I apologize if my question is basic. I've been given a new repository to work on, but I'm having trouble building it through the command line. When attempting to run npm install and npm start on my work p ...

Troubleshooting Angular error encountered during the execution of the 'ng build --prod' command

While developing my Angular application, I encountered an issue when using the ng build --prod command. Surprisingly, everything was working fine with ng serve. I am struggling to figure out what the problem could be. ERROR in: Unable to locate resource a ...

Navigating to a child route from a parent component in Angular using the navigate method: A step-by-step guide

Here is the code snippet for my HTML routerLink: <a [routerLink]="['create']">Create</a> - it's working just fine. When I try to call the same on a button click, it doesn't work.. navigateTo(page) { this.router.na ...

Unable to locate the angular/core and angular-router-deprecated modules within Angular 2

https://i.sstatic.net/pvTQA.png Furthermore, it is displaying an error indicating that the names 'Map' and 'Promise' cannot be found. What could be causing this issue? ...

What is the correct way to define types for higher-order class components in TypeScript?

I have developed a utility function that currently has an unused options parameter, and it returns a higher-order component wrapping function. How can I effectively define types on the component so that users of the wrapped component can visualize the typ ...

Dynamically setting attributes with ngFor iteration

I am attempting to dynamically set the selected attribute based on matching index values. <select class="form-control" disabled> <option *ngFor="let agency of request.agencyList" [attr.selected]="request.agencyIndex == agency">{{agency}}{{re ...

Tips for dynamically rendering a React component from an object

Looking to include an imported component with some props in my React code. Unable to find a solution on Stack Overflow for this specific format. Can someone provide guidance on how to achieve this? Thanks in advance. import { HomeIcon } from "../lib/i ...

How to control the audio currentTime using Angular 2 component

Here is the HTML code snippet that creates an HTML5 audio element: <audio id ="audio2" controls="controls" autoplay="false" (canplay)="CanPlay($event)"> <source src="http://localhost:51657/Audio/1" type="audio/mp3"> </audio> In the ...

Utilizing a single resolver for both parent and child routes in Angular 2

In my Angular 2.0 (stable version) application, I have a specific entity called project. The details of each project are spread across different sections/routes within the app: project/:id/overview project/:id/documents project/:id/logs and more The API ...

Disable editing of all form controls within a template-based form

We are looking to implement a 'read-only' feature for certain users in our template-based form that utilizes Angular Material controls such as matInput and mat-select. My initial approach was to check user privileges and then iterate through the ...

Angular 2 - update browsing history by replacing instead of adding to it

Is it possible to replace the history instead of pushing a new one in Angular 2's new router (rc.1)? For instance, suppose I am in a question list (/questions), then open a new modal in a new route (/questions/add). After adding a new question, I nav ...

Filtering database results from an Angular component

I am currently working on an Angular component and I have a result variable in the .ts file that stores data retrieved from the database. My goal is to filter this result variable to display only 20 records and sort them by date either in ascending or de ...

Why is it necessary to create a model in Angular?

Although I am relatively new to Angular and my understanding of all its concepts is limited, I am curious about the practical use of models in observables. For example, consider this code segment: getRestaurants = (): Observable<Restaurant[]> => ...

The 'state' value appears as undefined when utilizing useContext, yet it is not undefined within the

I've been exploring the concept of creating a versatile, reusable Provider component. However, I'm facing an issue where the state obtained from useContext is returning as undefined. Oddly enough, within the provider itself, the state is not unde ...

Steering clear of Endless Loops in Spring Boot

When utilizing a Spring Boot Rest API, I successfully prevented an infinite loop by using @JsonIgnore. However, in the postman response, the related list on the many side appears as null. How can I go about displaying this related list in Angular even if i ...

The routing navigate method is failing to direct to the desired component

For the past day, I have been struggling to find a solution to my issue but without success. The routing seems to be malfunctioning as it keeps showing the HTML content from app.component.html even when I try changing the path in the URL. Here is a snippe ...