Angular 2: getting RouteParams from parent component

前端 未结 13 912
暖寄归人
暖寄归人 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: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');
      }
    }
    

提交回复
热议问题