【问题标题】:Angular 4 error when navigating using secondary outlet使用辅助插座导航时出现 Angular 4 错误
【发布时间】:2017-06-05 13:22:08
【问题描述】:

我在使用辅助插座进行导航时遇到问题。

  • 我有一个 LayoutComponent 加载到主路由器插座中。
  • LayoutComponent 包含一个名为 content-outlet 的辅助出口

到目前为止,一切都很好,我可以导航到例如/accounts/1/overview 并让content-outlet 加载正确的组件(OverviewComponent)。但是,当我单击侧边栏中的链接导航到 /accounts/1/stats 路线时,我收到一个错误:

Error: Cannot activate an already activated outlet

如果我首先导航到/accounts/1/stats,然后尝试激活/accounts/1/overview,也会发生同样的事情。

布局组件:

<app-sidebar></app-sidebar>
<div class="topbar-content-wrapper">
  <app-topbar></app-topbar>
  <div class="content-wrapper">
    <router-outlet name="content-outlet"></router-outlet>
  </div>
</div>

侧边栏导航:

<a href="#" [routerLink]="[ '/accounts', accountId, 'overview' ]">Overview</a>
<a href="#" [routerLink]="[ '/accounts', accountId, 'stats' ]">Stats</a>

路线:

{
path: 'accounts/:accountid',
component: LayoutComponent,
canActivate: [AuthService],
children: [
  {
    path: 'stats',
    children: [
      {
        path: '',
        component: StatsComponent,
        outlet: 'content-outlet'
      }
    ]
  },
  {
    path: 'overview',
    children: [
      {
        path: '',
        component: OverviewComponent,
        outlet: 'content-outlet'
      }
    ]
  }
]

【问题讨论】:

  • 为什么导航栏链接上有 href="#"?
  • 我认为这不重要吗? @Jota.Toledo

标签: angular angular2-routing


【解决方案1】:

将您的路线更改为以下

{
   path: 'accounts/:accountid',
   component: LayoutComponent,
   canActivate: [AuthService],
},
{
    path: 'stats',
    component: StatsComponent,
    outlet: 'content-outlet'
 },
{
    path: 'overview',
    component: OverviewComponent,
    outlet: 'content-outlet'
}

还有你的侧边栏导航链接:

<a [routerLink]="[{ outlets: { content-outlet: ['overview'] } }]">Overview</a>
<a [routerLink]="[{ outlets: { content-outlet: ['stats'] } }]">Stats</a>

【讨论】:

  • 感谢您的建议。当我将 Routes 定义更改为您的建议时,我根本无法导航到该页面。我收到错误Error: Cannot match any routes. URL Segment: 'accounts/1/overview'并被重定向到我的默认路由/login
  • 我真的不认为您可以在路由定义中将辅助出口组件嵌套在根出口组件内,因为每个命名出口都应该有自己的一组路由和自己的组件。检查我更新的答案以了解我的意思。
  • 但在我必须更改加载到 content-outlet 中的组件之前,它运行良好?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2017-02-10
  • 2017-02-15
  • 2020-07-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-09-23
相关资源
最近更新 更多