Angular 2: getting RouteParams from parent component

前端 未结 13 862
暖寄归人
暖寄归人 2020-11-28 04:47

How do I get the RouteParams from a parent component?

App.ts:

@Component({
  ...
})

@RouteConfig([
  {path: \'/\', component: HomeCompo         


        
相关标签:
13条回答
  • 2020-11-28 05:15

    With RxJS's Observable.combineLatest, we can get something close to the idiomatic params handling:

    import 'rxjs/add/operator/combineLatest';
    
    import {Component} from '@angular/core';
    import {ActivatedRoute, Params} from '@angular/router';
    import {Observable} from 'rxjs/Observable';
    
    @Component({ /* ... */ })
    export class SomeChildComponent {
      email: string;
      id: string;
    
      constructor(private route: ActivatedRoute) {}
    
      ngOnInit() {
        Observable.combineLatest(this.route.params, this.route.parent.params)
            .forEach((params: Params[]) => {
              this.id = params[0]['id'];
              this.email = params[1]['email'];
            });
      }
    }
    
    0 讨论(0)
  • 2020-11-28 05:18

    I found an ugly but working solution, by requesting the parent (precisely the 2nd ancestor) injector, and by getting the RouteParams from here.

    Something like

    @Component({
      ...
    })
    export class ChildOneComponent {
      public username: string;
    
      constructor(injector: Injector) {
        let params = injector.parent.parent.get(RouteParams);
    
        this.username = params.get('username');
      }
    }
    
    0 讨论(0)
  • 2020-11-28 05:18

    RC5 + @angular/router": "3.0.0-rc.1 SOLUTION: It seems that this.router.routerState.queryParams has been deprecated. You can get the parent route params this way:

    constructor(private activatedRoute: ActivatedRoute) {
    }    
    
    this.activatedRoute.parent.params.subscribe(
      (param: any) => {
        let userId = param['userId'];
        console.log(userId);
      });
    
    0 讨论(0)
  • 2020-11-28 05:18

    In FINAL with little help of RXJS you can combine both maps (from child and parent):

    (route) => Observable
        .zip(route.params, route.parent.params)
        .map(data => Object.assign({}, data[0], data[1]))
    

    Other questions one might have:

    • Is it really a good idea to use above - because of coupling (couple child component with parent's param's - not on api level - hidden coupling),
    • Is it proper approach in term of RXJS (it would require hardcore RXJS user feedback ;)
    0 讨论(0)
  • 2020-11-28 05:20

    As mentioned by Günter Zöchbauer, I used the comment at https://github.com/angular/angular/issues/6204#issuecomment-173273143 to address my problem. I used the Injector class from angular2/core to fetch the routeparams of the parent. Turns out angular 2 does not handle deeply nested routes. Maybe they'll add that in the future.

    constructor(private _issueService: IssueService,
                private _injector: Injector) {}
    
    getIssues() {
        let id = this._injector.parent.parent.get(RouteParams).get('id');
        this._issueService.getIssues(id).then(issues => this.issues = issues);
    }
    
    0 讨论(0)
  • 2020-11-28 05:23

    You can take component of parent route inside of child component from injector and then get any from child component. In you case like this

    @Component({
      ...
    })
    
    export class ChildOneComponent {
    
      public username: string;
    
      constructor(
        public params: RouteParams
        private _injector: Injector
    
      ) {
        var parentComponent = this._injector.get(ParentComponent)
    
        this.username = parentComponent.username;
        //or
        this.username = parentComponent.params.get('username');
      }
    
      ...
    }
    
    0 讨论(0)
提交回复
热议问题