Angular 2: getting RouteParams from parent component

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

How do I get the RouteParams from a parent component?

App.ts:

@Component({
  ...
})

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


        
13条回答
  •  Happy的楠姐
    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);
    }
    

提交回复
热议问题