我有这条路线:
{
path: ':parent_id',
component: Parent,
children: [
{
path: 'child',
children: [
{
path: ':child_id',
component: Child
}
]
}
]
}
如何从子组件中检索parent_id值?
答案 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
}
});
}