角度中的父子路由

时间:2018-01-23 07:25:23

标签: angular routing angular-routing

当我以角度执行路由时,我收到Error: Cannot match any routes. URL Segment: 'example2/child1'这样的错误,我在app.module.ts中定义路由

{ path:"for", component: ForComponent },
      { path:"example1", component: Example1Component },
      { path:"example2",component: Example2Component ,
          children: [          
          { path:"child1",component:Child1Component },
          { path:"child2",component:Child2Component },
          { path:"child3",component:Child3Component }
          ]
      }      

在我的父组件中,我设置动态链接以导航子组件

<ul>
     <li *ngFor="let item of coutry">
         <a href="" routerLink="{{item.name}}" >{{item.name}}</a>
     </li>
</ul>

那我该怎么办?另外,请参阅StackOverflow中的相同问题但不起作用click here

2 个答案:

答案 0 :(得分:2)

如果groups.map(o=>({'title': o.name})) 数组中没有india作为路径,它如何匹配india。您需要它,或者像children这样的参数路径:

解决方案1:

path:":country"

或使用参数路径:

{ path:"example2",component: Example2Component ,
  children: [          
    { path:"india",component:Child1Component },
    // ...
  ]
} 

答案 1 :(得分:1)

您尚未输入'example2'子项的路径分辨率:

{ path:"for", component: ForComponent },
      { path:"example1", component: Example1Component },
      { path:"example2",component: Example2Component ,
          children: [          
          { path:"india",component:Child1Component },
          { path:"child2",component:Child2Component },
          { path:"child3",component:Child3Component }
          ]
      }