Angular 6 - multiple router outlets

MarcoLe picture MarcoLe · Jul 7, 2018 · Viewed 23.2k times · Source

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>

Answer

Avij picture Avij · Jul 7, 2018

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'
 }