角度路由,子路由和默认路由

时间:2017-06-02 12:09:07

标签: angular module single-page-application router-outlet

我正在尝试使用带模块的路线......

app.module

  {
      path: '',
      component: AppComponent,
      children: [
          { path: '', redirectTo: 'dashboard', pathMatch: 'full' },
          { path: 'dashboard', loadChildren: 'app/dashboard/dashboard.module#DashboardModule'
      ]
  }

使用导入RouterModule.forRoot(appRoutes)

dashboard.module

  {
      path: '',
      component: DashboardComponent,
      children: [
          { path: '', redirectTo: 'conta', pathMatch: 'full' },
          { path: 'conta', loadChildren: 'app/dashboard/conta/conta.module#ContaModule' }
      ]
  }

使用导入RouterModule.forChild(dashboardRoutes)

conta.module

  {
      path: '',
      component: ContaComponent,
      children: [
          { path: '', redirectTo: 'list', pathMatch: 'full' },
          { path: 'list', component: ContaListComponent }
      ]
  }

使用导入RouterModule.forChild(contaRoutes)

这个想法是:

  • 应用程序的默认路线是仪表板
  • 到仪表板的默认路线是conta
  • 到conta的默认路由是contaList

当我运行此代码时,应用正在加载应用> Conta> ContaList 而非 App>仪表板> Conta> ContaList 我的意愿。

我的模板(App,Dashboard和Conta)里面有一个路由器插座。

我做错了什么?

1 个答案:

答案 0 :(得分:1)

在仪表板组件中,您需要将路径指定为仪表板,并在conta模块中指定相同的

{
      path: 'dashboard',
      component: DashboardComponent,
      children: [
          { path: '', redirectTo: 'conta', pathMatch: 'full' },
          { path: 'conta', loadChildren: 'app/dashboard/conta/conta.module#ContaModule' }
      ]
  }


{
      path: 'conta',
      component: ContaComponent,
      children: [
          { path: '', redirectTo: 'list', pathMatch: 'full' },
          { path: 'list', component: ContaListComponent }
      ]
  }
相关问题