2017-08-03 79 views
0

这是我第二次在Angular路由器中遇到一些好奇的事情。 我目前正在使用Angular v4.3.1。儿童路线参数不可用

我使用相同的组件来创建和编辑对象。 这就是我想为这个组件定义两条路由的原因,一个使用了id,另一个没有。

我试图创建以下路线:

{ 
    path: 'company/settings/survey', 
    component: SurveyComponent, 
    children:[ 
     {path:':id', 
     component:SurveyComponent 
     } 
    ] 
} 

但这种情况下,我的ID参数是从未设置。

this.route.paramMap.subscribe((params) => { 
    if (params.has('id')) { 
     //Some stuff 
    } 
    } 

我目前使用故障恢复:

我与检索它

{ 
    path: 'company/settings/survey', 
    component: SurveyComponent, 
    }, { 
    path:'company/settings/survey/:id', 
    component: SurveyComponent 
    } 

,但我想了解我的第一个解决方案是不工作的原因。

感谢您的帮助!

回答

1

内试试这个:

{ 
    path: 'company/settings/survey', 
    children:[ 
     { 
      path:'', component:SurveyComponent 
     }, 
     { 
      path:':id', component:SurveyComponent 
     }, 
    ] 
} 

然后

constructor(route: ActivatedRouteSnapshot) {} 
this.route.paramMap.get('id'); 
....... 
+0

嗯,这是很好的! 有了这个答案以及@justin提供的解释,我更好地理解了我得到的行为,以及我得到的结果! –

1

在您的第一个示例中,您的代码将尝试路由到另一个SurveyComponent内的SurveyComponent,我不认为这是您打算执行的操作。

子路由被装入<router-outlet>父组件