从其他插座获取插座参数
Get outlet params from other outlet
我有以下 url 结构:
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
内,如何从 bar
出口检索 barId
参数?
使用这个:
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'];
}
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 }
// ]
我有以下 url 结构:
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
内,如何从 bar
出口检索 barId
参数?
使用这个:
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'];
}
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 }
// ]