Angular 6-多个路由器插座

时间:2018-07-07 12:26:26

标签: angular routing angular2-routing router-outlet

我想要一个Enter-comp。作为概述,基本上有两个child-comp。例如,登录和注册补偿。此外,我想通过多个路由器插座进行管理。但是“当然”它还行不通。它使我不断抛出:Error: Cannot match any routes. URL Segment: ',%20%7Boutlets:%20%7Blogin:%20%5B'login'%5D%7D%7D'

应用程序路由配置

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

应用程序根

<app-navbar></app-navbar>

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

Navbar comp。

在这里预期错误。我怀疑是我打错了路线:

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

2 个答案:

答案 0 :(得分:4)

问题是您正在尝试将辅助路由作为字符串发送。 routerLink以字符串形式评估RHS上的值。这就是为什么您会在路线中看到奇怪的字符的原因。为此,您需要尝试以下操作-

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

以您的情况

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

已编辑

相对于ROOT路径的具名子出口不起作用,看起来像BUG

comments section中提到了一种解决方法。

OR

您可以更改如下配置-

{
    path: '', component: LoginComponent
 },
 {
    path: 'example1', component: ExampleComponent, outlet: 'example'
 }

答案 1 :(得分:0)

请参考以下示例

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