我有以下网址结构:
http://localhost:4200/foo/:fooId/(bar:barId//baz:bazId)
以下路由器配置:
{
path: 'foo',
children: [
{
path: ':fooId',
component: fooComponent,
children: [
{
path: ':fooId',
component: FooComponent
},
{
path: ':barId',
component: BarComponent,
outlet: 'bar'
},
{
path: ':bazId',
component: BazComponent,
outlet: 'baz'
}
]
}
]
}
如果我在http://localhost:4200/foo/0/(bar:1//baz:2)
,BazComponent
内,我该如何从barId
商店检索bar
参数?
答案 0 :(得分:1)
import { ActivatedRoute } from '@angular/router';
params: any[] = [];
constructor(private route:ActivatedRoute) {
this.route.parent.children.forEach(children => {
this.params.push(children.snapshot.params);
});
}
ngOnInit() {
console.log(params);
}
// This returns:
// [
// { bar: barId },
// { baz: bazId }
// ]
答案 1 :(得分:0)
使用此:
import {ActivatedRoute} from '@angular/router';
constructor(private route:ActivatedRoute){}
barId:number;
ngOnInit(){
// 'bar' is the name of the route parameter
this.barId = this.route.snapshot.params['bar'];
}