简体   繁体   English

带孩子的Angular 2条路线

[英]Angular 2 routes with children

I'm trying to create a default route (home) and I want "home" to have childroutes. 我正在尝试创建默认路由(家),并且我希望“家”具有子路由。 So that when you go to Home, the homecomponent should load, and when you go to Homes children, a specific childroute should load. 因此,当您转到Home时,应加载home组件,而转到Homes子级时,应加载特定的子路由。 But right now my home component loads, but I can't navigate to any of it's children. 但是现在我的home组件已加载,但我无法导航到其中的任何子组件。 What am I doing wrong? 我究竟做错了什么?

This is my core routes: 这是我的核心路线:

export const coreRoutes: Routes = [
  { 
    path: '', 
    redirectTo: 'home', 
    pathMatch: 'full' 
  },
  { 
    path: 'home', 
    component: HomeComponent, 
    data: { breadcrumb: 'Home', icon: 'fa-home' }, 
    children: homeChildRoutes 
  }
];

and this is my homeChildRoutes: 这是我的homeChildRoutes:

export const homeChildRoutes: Routes = [
  { 
    path: '', 
    redirectTo: 'home', 
    pathMatch: 'full' 
  },
  { 
    path: 'first-child', 
    component: firstChildComponent, 
    data: { breadcrumb: 'First Child', parent: 'home' } 
  },
  { 
    path: 'second-child', 
    component: secondChildComponent, 
    data: { breadcrumb: 'Second Child', parent: 'home'  } 
  }
];

When you're on /home page, HomeComponent is shown /home页面上时,将显示HomeComponent

When you're on /home/first-child page, firstChildComponent is shown, HomeComponent is hidden /home/first-child页面上时,显示firstChildComponent,而HomeComponent隐藏

// core routes
export const coreRoutes: Routes = [
  { 
    path: 'home', 
    children: homeChildRoutes
  },
  { 
    path: '', 
    redirectTo: 'home', 
    pathMatch: 'full' 
  },
];

// child routes
export const homeChildRoutes: Routes = [
  {
    path: '',
    component: HomeComponent,
    data: { breadcrumb: 'Home', icon: 'fa-home' },
  },
  { 
    path: 'first-child', 
    component: firstChildComponent, 
    data: { breadcrumb: 'First Child', parent: 'home' } 
  },
  { 
    path: 'second-child', 
    component: secondChildComponent, 
    data: { breadcrumb: 'Second Child', parent: 'home'  } 
  },
];

Routes can be simplified to this: 路线可以简化为:

export const coreRoutes: Routes = [
  { 
    path: 'home', 
    component: HomeComponent, 
    data: { breadcrumb: 'Home', icon: 'fa-home' }, 
    children: [
      { 
         path: '', 
         redirectTo: 'home', 
         pathMatch: 'full' 
      },
      { 
        path: 'first-child', 
        component: firstChildComponent, 
        data: { breadcrumb: 'First Child', parent: 'home' } 
      },
      { 
        path: 'second-child', 
        component: secondChildComponent, 
        data: { breadcrumb: 'Second Child', parent: 'home'  } 
      }
    ] 
  },
  ,
 { 
    path: '', 
    redirectTo: 'home', 
    pathMatch: 'full' 
 }
];

Make sure your HomeComponent has a router-outlet. 确保您的HomeComponent具有路由器出口。 Or else your child component will not be visible. 否则您的子组件将不可见。

Refer this for code. 请参考代码。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM