我想从我的url获取参数。我的路线结构如下
{
path: 'customers/viewCustomer/:id',component:CommonSingleCustomerPageComponent, canActivate: [AuthGuard], data:{ permission:['Manage-Customer']},
children: [
{
path: 'detail',
component: ViewCustomerComponent, canActivate: [AuthGuard],data:{permission: ['Manage-Customer']}
},
}
但当我试图在viewcustomercomponent中获取:id时,我会将其视为未定义。我尝试过以下方法:
export class ViewCustomerComponent implements OnInit {
constructor(
private router: ActivatedRoute,
) { }
data: any = '';
id: any = '';
ngOnInit(): void {
this.id = this.router.snapshot.params.id
console.log(this.id)
}
}
而且,
export class ViewCustomerComponent implements OnInit {
constructor(
private router: ActivatedRoute,
) { }
data: any = '';
id: any = '';
ngOnInit(): void {
this.id = this.router.snapshot.paramMap.get('id')
console.log(this.id)
}
}
但这些都不管用。你知道我做错了什么吗?
1条答案
按热度按时间wvt8vs2t1#
您希望访问在父级中定义的路径路由。因此,使用父对象访问子组件viewcustomercomponent中的值(:id)
或者参考本文https://ultimatecourses.com/blog/angular-parent-routing-params