0

In app-routing I have one parent component and 3 childrens:

{
   path: 'media', component: MediaComponent,
   children: [
      { path: '', redirectTo: 'photos', pathMatch: 'full' },
      { path: 'photos', component: PhotosComponent },
      { path: 'documents', component: DocumentsComponent },
      { path: 'links', component: LinksComponent }
     ]
}

And in MediaComponent I get a route param like this: this.activatedRoute.parent.snapshot.params.memberId;

In any other components I tried the same thing and it is undefined. How can i get these params to childrenns?

1 Answers1

0

I am not sure why you have access to this parameter in the MediaComponent, but you can try for the children the following:

{ path: 'photos', component: PhotosComponent, resolve { memberId: <value> } }

Then access it with:

this.route.snapshot.data.memberId.value

BR

Update:

@Injectable()
export class MemberIdResolver implements Resolve<number> {
  constructor(private yourService: yourService) {}

  resolve(route: ActivatedRouteSnapshot): number {
    return this.yourService.getMemberId();
  }
}

Then in the app-routing.module.ts:

{ path: 'photos', component: PhotosComponent, resolve { memberId: MemberIdResolver } }

Then you can access is it in the component as I mentioned before:

this.route.snapshot.data.memberId.value
Vitomir
  • 335
  • 2
  • 15
  • And how should I get my activated route in app-routing? Or what should value contain? – Andrei Kruza Dec 07 '21 at 09:11
  • @AndreiKruza depends on what value you want to put there, I have made the following Resolver where I am calling a method from one of my services which returns an array of objects, but in your case it might be something like: `ts @Injectable() export class MemberIdResolver implements Resolve { constructor(private yourService: YourService) {} resolve(route: ActivatedRouteSnapshot): number { return this.yourService.getMemberId(); } } ` – Vitomir Dec 07 '21 at 09:19