Angular 2路由具有相同的URL?

时间:2016-01-11 22:22:51

标签: angular angular2-routing

与Angular UI路由器一样,Angular2中是否可以有多个具有相同URL的路由?例如,遵循两条路线:

{ path: '/home', name: 'Home', component: HomeComponent} ,
{ path: '/home', name: 'Dashboard', component: DashboardComponent}

目前,控制台中存在错误路由错误。

2 个答案:

答案 0 :(得分:1)

如果您希望AuxRoute样式命名为ui-router,则可以使用ui-view

https://angular.io/docs/ts/latest/api/router/AuxRoute-class.html

另外,请参阅此问题。 Is Angular 2 Auxiliary router broken?

示例:http://plnkr.co/edit/JsZbuR?p=preview

答案 1 :(得分:-1)

您可以查看Angular2 - two components with the same route讨论可能的解决方案,以实现您的目标。

  1. 根据评估的条件,在模板中使用* ngIf来使用不同的选择器。

    <home-logged-in *ngIf="authenticated()"></home-logged-in>
    <home-logged-out *ngIf="!authenticated()"></home-logged-out>
    
  2. 创建自定义路由器插座,根据评估的条件加载特定组件。

  3. 根据评估的条件重定向到不同的路线(参见来源http://www.captaincodeman.com/2016/03/31/angular2-route-security/

  4. 此列表不具有决定性,可能还有其他方法可以实现您的目标。

相关问题