question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Routing to named outlets doesn't work with non-empty paths

See original GitHub issue

Somebody, create another issue with the representation, or it won't be fixed. I can't. Sorry

I’m submitting a … (check one with “x”)

[x] bug report => search github for a similar issue or PR before submitting
[ ] feature request
[ ] support request => Please do not submit support request here, instead see https://github.com/angular/angular/blob/master/CONTRIBUTING.md#question

Current behavior

In case of a named router-outlet, routing works only with empty paths. This is written to the browser console: Error: Cannot match any routes. URL Segment: ‘a’

Expected behavior

Routing should create components whether the outlet is named or not.

Minimal reproduction of the problem with instructions

Here’s a code for a simple routing app. It’s got three paths (/, /a, /b) and a button for each. The paths are routed to components. The code has got several Routes configurations. Some of them are working (navigating to a know path renders a component), others are bugged.

import {Component, NgModule} from '@angular/core';
import {BrowserModule} from '@angular/platform-browser';
import {RouterModule, Routes} from '@angular/router';

@Component({
  selector: 'our-main',
  template: `
    <a href="">Navigate to /</a>
    <a href="a">Navigate to /a</a>
    <a href="b">Navigate to /b</a>
    <div>router-outlet name='the-out':</div>
    <router-outlet name="the-out"></router-outlet>
    <div>router-outlet, name='':</div>
    <router-outlet></router-outlet>
  `,
  styles: ['a { display: block; }']
})
class MainComponent {}

@Component({ selector: 'our-default', template: '<h1>Default</h1>' })
class DefaultComponent {}

@Component({ selector: 'our-a', template: '<h1>A</h1>', })
class AComponent {}

@Component({ selector: 'our-b', template: '<h1>B</h1>' })
class BComponent {}

const ROUTES_OK_1: Routes = [
  { path: '', component: DefaultComponent, outlet: 'the-out', pathMatch: 'full'},
  { path: 'a', component: AComponent, outlet: ''},
  { path: 'b', component: BComponent, outlet: ''}
];

const ROUTES_OK_2: Routes = [
  { path: '', component: DefaultComponent, outlet: 'the-out'},
  { path: 'a', component: AComponent, outlet: ''},
  { path: 'b', component: BComponent, outlet: ''}
];

const ROUTES_OK_3: Routes = [
  { path: '', component: DefaultComponent, outlet: '', pathMatch: 'full'},
  { path: 'a', component: AComponent, outlet: ''},
  { path: 'b', component: BComponent, outlet: ''}
];

const ROUTES_BUG_1: Routes = [
  { path: '', component: DefaultComponent, outlet: 'the-out', pathMatch: 'full'},
  { path: 'a', component: AComponent, outlet: 'the-out'},
  { path: 'b', component: BComponent, outlet: 'the-out'}
];

const ROUTES_BUG_2: Routes = [
  { path: '', component: DefaultComponent, outlet: '', pathMatch: 'full'},
  { path: 'a', component: AComponent, outlet: 'the-out'},
  { path: 'b', component: BComponent, outlet: 'the-out'}
];

@NgModule({
  imports: [BrowserModule, RouterModule.forRoot(ROUTES_BUG_1)],
  declarations: [MainComponent, DefaultComponent, AComponent, BComponent],
  bootstrap: [MainComponent]
})
export class AppModule {}

What is the motivation / use case for changing the behavior?

I don’t know. I’m just learning. I can’t even tell why there’re named router-outlets at all. The developers should know better. Please tell us about your environment: Linux Mint 17.3 IDE IntellijIDEA 2017.1.2 package manager yarn HTTP server lite-server

  • Angular version: 4.0.0
  • Browser: Firefox 53
  • Language: TypeScript 2.1.0
  • Node (for AoT issues): node --version = 7.4.0

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Reactions:11
  • Comments:17 (3 by maintainers)

github_iconTop GitHub Comments

10reactions
kamranahmedsecommented, Sep 6, 2017

I have the same issue, component doesn’t get added to the outlet for non-empty paths i.e. below does not work

{
    path: 'some-path',
    component: SidebarComponent,
    outlet: 'sidebar'
}

But it does work for the non-empty paths. For now I have been using the below stated hack to achieve it, since it seems to be working when the path is empty.

{
    path: 'some-path',
    children: [
        {
            path: '',
            component: SidebarComponent,
            outlet: 'sidebar'
        }        
    ]
}

I am not sure if it is by design or not as I am not able to find anything regarding this in the documentation. Would love a word from the core team? /@vicb

PS, I have it in one of the feature modules that is lazy loaded and haven’t tried it in the parent module so I am not sure about the behavior in parent module.

5reactions
webmatrixxxlcommented, Sep 14, 2017

Same problem with angular 4.3.6

Read more comments on GitHub >

github_iconTop Results From Across the Web

Named-Outlets Require Non-Empty Parent Route-Segment ...
Ben Nadel demonstrates that secondary, named route outlets must have non-empty parent route segments paths in Angular 4.4.4.
Read more >
Angular 11 cannot route to named outlet - Stack Overflow
Your home component has empty path which does not work with named outlets. They work only if the path of the of the...
Read more >
Andrei Gătej
Angular Router: empty paths, named outlets and a fix that came with Angular 11 ... In this article, we are going to highlight...
Read more >
RouterOutlet - Angular
Named outlets can be the targets of secondary routes. The Route object for a secondary route has an outlet property to identify the...
Read more >
Angular Router: Child Routes, Auxiliary Routes, Master Detail
We can see that the empty path configuration would map the URL / to the component Home , and all other paths to...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found