【问题标题】:Angular 2 - My sub routes dont open nothingAngular 2 - 我的子路线没有打开任何东西
【发布时间】:2018-10-21 03:25:55
【问题描述】:

我的应用程序中有两条路线.. 一条在另一条中,例如:Image

当点击子路由时,什么也没有发生

我的代码:

App.route.ts:

export const routes: Routes = [
    {path: '', loadChildren: './dashboard/dashboard.module#DashboardModule', component: DashboardComponent},
    {path: 'profile', component: ProfileComponent}
];

App.module.ts:

@NgModule({
  declarations: [
    AppComponent
  ],
  imports: [
    RouterModule.forRoot(routes)
  ],
...

App.component.html:

<nav>...
<router-outlet></router-outlet>

Dashboard.routes.ts:

export const dashboard: Routes = [
  {path: '', component: DashboardComponent,
    children: [
      {path: '', component: EscapeGameComponent, outlet: 'subOutlet'},
      {path: 'boardGame', component: BoardGameComponent, outlet: 'subOutlet'}
    ]
  }
];

Dashboard.module.ts

@NgModule({
  imports: [
    RouterModule.forChild(dashboard)
  ],
...

Dashboard.component.html

  <p [routerLink]="[{ outlets: { subOutlet: [''] } }]">Escape Game</p>
  <p [routerLink]="[{ outlets: { subOutlet: ['boardGame'] } }]">Board Game</p>
  <router-outlet name="subOutlet"></router-outlet>

为什么当我点击&lt;p&gt; 时,什么都没有发生?

【问题讨论】:

    标签: angular routing angular2-routing


    【解决方案1】:

    您有几个错误的配置和不需要的 subOutlet。

    dashboard.routes.ts

    export const dashboard: Routes = [
      {path: '', component: DashboardComponent,
        children: [
          {path: '', component: EscapeGameComponent},
          {path: 'boardGame', component: BoardGameComponent}
        ]
      }
    ];
    

    dashboard.component.html

      <p [routerLink]="['/']">Escape Game</p>
      <p [routerLink]="['/boardGame']">Board Game</p>
      <router-outlet></router-outlet>
    

    【讨论】:

    • 很抱歉,它起作用了,我错了,但它仍然不起作用..现在的问题是子程序可以工作,但主要的没有..我可以导航棋盘游戏和逃脱游戏,但不在主要路线和个人资料之间
    • 更改顺序 'export const routes: Routes = [ {path: '', loadChildren: './dashboard/dashboard.module#DashboardModule', component: DashboardComponent}, {path: 'profile' , 组件: ProfileComponent} ];'
    • 是的!现在它起作用了!非常感谢,但是在这种情况下,命令会干扰什么?
    • Angular 路由器从第一个开始搜索到下一个。由于在您当前的配置中path: '' 将与所有路由匹配,因为它是空的'',它将停止移动到下一个路径。您还有其他选择可以让pathMatch: 'full' 处理此问题。
    【解决方案2】:

    -向你的父路由添加一个children 属性,这是一个路由数组,你必须在父元素内有一个router-outlet,例如:

    export const routes: Routes = [
     {
      path: 'profile',
      component: ProfileComponent,
      children: [
       {path: 'settings', component: ProfileSettingsComponent}
       {path: 'followers', component: ProfileFollowersComponent}
      ]
     }
    ];
    

    如果出口是您组件中唯一的出口,那么您不必命名它。

    【讨论】:

    • 感谢克里姆!我使用了 Sunil 的答案,它奏效了。但它也有效
    • 很抱歉,它起作用了,我错了,但它仍然不起作用..现在的问题是子程序可以工作,但主要的没有..我可以导航棋盘游戏和逃脱游戏,但不在主要路线和个人资料之间
    猜你喜欢
    • 1970-01-01
    • 2015-06-29
    • 2016-10-04
    • 2011-10-21
    • 1970-01-01
    • 2020-01-10
    • 1970-01-01
    • 1970-01-01
    • 2016-07-09
    相关资源
    最近更新 更多