角嵌套路由父级到子级变量

时间:2018-10-09 13:40:23

标签: angular

我有这条路线:

{ 
  path: ':parent_id',
  component: Parent, 
  children: [
    {
      path: 'child',
      children: [
        {
          path: ':child_id',
          component: Child
         }
      ]
    }
  ]
}

如何从子组件中检索parent_id值?

1 个答案:

答案 0 :(得分:2)

您应该可以使用ActivatedRoute

进行访问

首先将其导入:import {ActivatedRoute} from "@angular/router";

然后尝试如下操作:

constructor(private route: ActivatedRoute) {

  this.route.parent.params.subscribe((params) => {
    console.log(params); // This would show you all the available params

    if (params['parent_id']) { 
      console.log(params['term']) // Prints the console parent_id param
    }
  });

}