从其他商店获得插座

时间:2017-11-22 09:27:23

标签: angular2-routing

我有以下网址结构:

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参数?

2 个答案:

答案 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'];
    }