Angular 2: getting RouteParams from parent component

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

How do I get the RouteParams from a parent component?

App.ts:

@Component({
  ...
})

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


        
13条回答
  •  萌比男神i
    2020-11-28 05:08

    Getting RouteParams from parent component in Angular 8 -

    I have a route http://localhost:4200/partner/student-profile/1234/info

    Parent route - student-profile

    Param - 1234 (student_id)

    Child route - info


    Accessing param in child route (info) -

    Import

    import { ActivatedRoute, Router, ParamMap } from '@angular/router';
    

    Constructor

    constructor(private activatedRoute: ActivatedRoute, private router: Router) { }
    

    Accessing parent route params

    this.activatedRoute.parent.paramMap.subscribe((params: ParamMap) => this.studentId = (params.get('student_id')));
    


    Now, our variable studentId has the param value.

提交回复
热议问题