Angular 访问子模块中的路由ID
我对Angular routing非常陌生,正在尝试从该路由访问ID,但似乎没有可用的参数。以下是我的路线的组织方式(在app.module.ts中导入): 我的Angular 访问子模块中的路由ID,angular,routing,Angular,Routing,我对Angular routing非常陌生,正在尝试从该路由访问ID,但似乎没有可用的参数。以下是我的路线的组织方式(在app.module.ts中导入): 我的FinancePageModule没有路由(这可能是我无法访问ID的原因),加载FinancePage时,ID为空 export class FinancePage implements OnInit { client_id: number; constructor( private route: ActivatedRout
FinancePageModule
没有路由(这可能是我无法访问ID的原因),加载FinancePage
时,ID为空
export class FinancePage implements OnInit {
client_id: number;
constructor( private route: ActivatedRoute ) {
}
ngOnInit() {
this.client_id = parseInt(this.route.snapshot.paramMap.get('id'));
console.log(this.client_id) // logs NaN
}
}
如何修改我的路线,使此变量在
FinancePage
组件中可用?在您的情况下,在父路线中设置id
,以便您可以使用this.route.parent
原因是ActivateRoute仅通过
当前组件管线的参数,因为
财务没有任何路由参数它没有传递任何参数
因此,您必须使用route.parent
获取父路由的参数
ngOnInit() {
this.client_id = parseInt(this.route.parent.snapshot.paramMap.get('id'));
this.route.parent.params.forEach(
param => console.log(param)
)
this.route.parent.paramMap.subscribe(params => {
console.log(params)
})
}
当我尝试这样做时,
this.route.parent
为null
。我认为我的路线设置方式有问题,请尝试为我们提供stackblitz链接和您的运行代码
ngOnInit() {
this.client_id = parseInt(this.route.parent.snapshot.paramMap.get('id'));
this.route.parent.params.forEach(
param => console.log(param)
)
this.route.parent.paramMap.subscribe(params => {
console.log(params)
})
}