如何通过URL导航加载子路线?

时间:2019-09-02 07:30:58

标签: angular

我有一条子路线。我尝试将路线转到该子路线,但没有成功。如果我将此路由放置在children数组之外(作为独立路由),那么它将起作用,但不能在children数组中。路由器出口标签也位于父HTML模板中。我在做什么错了?

  {path:'adminpanel',component:AdminPanelComponent,canActivate:[AuthGuard],data :{permittedRoles:['Admin']},
  children:[
//it des not work here
    {path:'specific-role-section/:roleId' , component:SpecificRoleSectionComponent}
  ]},
//it works here
  // {path:'specific-role-section/:roleId' , component:SpecificRoleSectionComponent}


//the caller method
 openRoleSection(roleId:string)
  {
    // this.router.navigateByUrl(`specific-role-section/${roleId}` ,  {relativeTo: this.activatedRoute})
    this.router.navigate([`specific-role-section/${roleId}`] ,  {relativeTo: this.activatedRoute})
  }


1 个答案:

答案 0 :(得分:1)

您的调用方方法上似乎出现问题。 修改导航方法如下。

//the caller method
 openRoleSection(roleId:string)
  { 
    // Try this way
    this.router.navigateByUrl(`adminpanel/specific-role-section/${roleId}` ,  {relativeTo: this.activatedRoute});

    this.router.navigate([`adminpanel/specific-role-section/${roleId}`] ,  {relativeTo: this.activatedRoute})
  }