7

I want to have an Enter-comp. as overview and basically two child-comp. For instance a login- and register-comp. Furthermore I want to manage this with multiple router outlets. But "of course" it doesnt work yet. It keeps me throwing: Error: Cannot match any routes. URL Segment: ',%20%7Boutlets:%20%7Blogin:%20%5B'login'%5D%7D%7D'

App routing config

const routes: Routes = [
  {path: 'stories', component: StoriesComponent},
  {path: '', component: EnterOverviewComponent, children: [
      {path: 'login', component: LoginComponent, outlet: 'login'},
      {path: 'register', component: RegisterComponent, outlet: 'register'},
    ]}
];

App-root

<app-navbar></app-navbar>

<router-outlet></router-outlet>
<router-outlet name="login"></router-outlet>
<router-outlet name="register"></router-outlet>

Navbar comp.

Expecting the error here. I suspect, that I call the false route:

<ul class="navbar-nav ml-auto">
  <li class="nav-item" routerLinkActive="active">
    <a class="nav-link" routerLink="/, {outlets: {login: ['login']}}">Sign In</a>
  </li>
  <li class="nav-item" routerLinkActive="active">
    <a class="nav-link" routerLink="/, {outlets: {register: ['register']}}">Register</a>
  </li>
</ul>
MarcoLe
  • 2,309
  • 6
  • 36
  • 74

2 Answers2

6

The problem is you are trying to send an Auxilliary route as a string. routerLink evaluates the value on RHS as string. That is why you get to see strange characters in your route. To make this work, you need to try as follows -

<a [routerLink]="[{ outlets: { example: ['example1'] } }]">Example</a>

In your case

 <a [routerLink]="[{ outlets: { register: ['register'] } }]">Example</a>//outletName:['route-path']

EDITED

Named Child outlet relative to ROOT route does not work and seems like a BUG

A workaround is mentioned in the comments section.

OR

You can change configs like below -

{
    path: '', component: LoginComponent
 },
 {
    path: 'example1', component: ExampleComponent, outlet: 'example'
 }
Avij
  • 684
  • 7
  • 17
  • 2
    Hm, I definately see your point, but with your approach it throws me another error...: ` URL Segment: 'register' Error: Cannot match any routes. URL Segment: 'register' at ...` Am I doint smth wron with the route configuration above? I dont get it... – MarcoLe Jul 07 '18 at 18:11
  • I think as you are relative to root route, it may not work, See BUG https://github.com/angular/angular/issues/18271#issuecomment-348071491. As a workaround, you can change configs like - https://github.com/angular/angular/issues/18271#issuecomment-398375172 – Avij Jul 08 '18 at 02:10
1

Please refer the below example

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

@Component({
selector: 'my-app',
template: 
`<a routerLink='A'>A</a>
<a routerLink='B'>B</a>
<br>
<a [routerLink]='[{ outlets: { secondRouter: ["C"] } }]'>C</a>
<a [routerLink]='[{ outlets: { secondRouter: ["D"] } }]'>D</a>

<router-outlet></router-outlet>
<router-outlet name='secondRouter'></router-outlet>`,
})
export class AppComponent  {}

/***** fitst router */
@Component({
selector: 'app-a',
template: '<h1>A</h1>',
})
export class AComponent  {}

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

 /****** second router */

@Component({
selector: 'app-c',
template: '<h1>C</h1>',
})
export class CComponent  {}

@Component({
selector: 'app-d',
template: '<h1>D</h1>',
})
export class DComponent  {}

const routes: Routes = [
 { path: 'A', component: AComponent },
 { path: 'B', component: BComponent },
 { path: 'C', component: CComponent, outlet: 'secondRouter' },
 { path: 'D', component: DComponent, outlet: 'secondRouter' }
 ];

 @NgModule({
imports: [RouterModule.forRoot(routes)],
exports: [RouterModule]
})
export class RootRoutingModule { }
murthy naika k
  • 559
  • 6
  • 12