在子组件中获取 angular 个路由参数
get angular route parameters in child components
我很难正确处理子组件的路由参数。
我有一个这样的路由参数:
{ path: 'bin-view/:pileid', loadChildren: () => import(`./bin-view/bin-view.module`).then(m => m.BinViewModule) }
和我传入参数的link:
<a [routerLink]="['/bin-view', _pileid]" routerLinkActive="active">view bin</a>
然后在我的子组件上下文中,我有以下内容:
export class OverviewComponent implements OnInit {
private _pileid;
constructor(private _activatedRoute:ActivatedRoute) {
this._pileid = this._activatedRoute.snapshot.paramMap.get('pileid');
}
ngOnInit() {
console.log(this._pileid);
}
}
但这一直给我一个 null
值。我在这里错过了什么?
使用[=h11=]代替snapshot.paramMap
constructor(private route: ActivatedRoute) { }
ngOnInit() {
this.route.parent.params.subscribe(params => {
this.pileid = params[pileid];
});
}
我很难正确处理子组件的路由参数。
我有一个这样的路由参数:
{ path: 'bin-view/:pileid', loadChildren: () => import(`./bin-view/bin-view.module`).then(m => m.BinViewModule) }
和我传入参数的link:
<a [routerLink]="['/bin-view', _pileid]" routerLinkActive="active">view bin</a>
然后在我的子组件上下文中,我有以下内容:
export class OverviewComponent implements OnInit {
private _pileid;
constructor(private _activatedRoute:ActivatedRoute) {
this._pileid = this._activatedRoute.snapshot.paramMap.get('pileid');
}
ngOnInit() {
console.log(this._pileid);
}
}
但这一直给我一个 null
值。我在这里错过了什么?
使用[=h11=]代替snapshot.paramMap
constructor(private route: ActivatedRoute) { }
ngOnInit() {
this.route.parent.params.subscribe(params => {
this.pileid = params[pileid];
});
}