Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Angular pass resolve data to child-routes

Tags:

i have the following Component Structure

  • project
    • edit-project
    • sub-ressource1
    • sub-ressource2
    • sub-ressource3

So my routing looks like this:

const childroutes = [   {     path: '',     children: [       { path: 'edit', component: EditProjectComponent},       { path: 'subres1', component: Subres1Component},       { path: 'subres2', component: Subres2Component},       { path: 'subres3', component: Subres3Component},       { path: 'subres4', component: Subres4Component}     ]   } ]  {     path: 'project/:projectId',      component: ProjectDetailComponent,     children: childroutes,     resolve: { project: ProjectResolver} /** resolve Project from ProjectService **/ } 

As you can see i resolve my Projectdata from a service and can access them in ProjectDetailComponent via

this.route.snapshot.data 

So now the question is, how can i pass the data resolved in "EditProjectComponent" to all its childroutes components?

I now that i could do the following to resolve project-data in childcomponents:

const childroutes = [   {     path: '',     children: [       { path: 'edit', component: EditProjectComponent,resolve: { project: ProjectResolver}},       { path: 'subres1', component: Subres1Component,resolve: { project: ProjectResolver}},       { path: 'subres2', component: Subres2Component,resolve: { project: ProjectResolver}},       { path: 'subres3', component: Subres3Component,resolve: { project: ProjectResolver}},       { path: 'subres4', component: Subres4Component,resolve: { project: ProjectResolver}}     ]   } ] 

But this seems ugly and wrong.

like image 818
enno.void Avatar asked Feb 02 '18 10:02

enno.void


People also ask

Can we pass data in router outlet in Angular?

There are various ways by which we can pass data from one component to another. Angular 7.2. 0 introduced a new way to pass data using State Object while navigating between routed components.

What is Route resolver in Angular?

So what is Angular Resolver? Angular Route Resolver is used for pre-fetching some of the data when the user is navigating from one route to another. It can be defined as a smooth approach for enhancing user experience by loading data before the user navigates to a particular component.


1 Answers

You have two options here:

1. You can access parent resolve data via the child's resolver by creating a child-specific resolver and accessing the route's parent property.

[...module.ts | ...component.ts]

{     path: 'project/:projectId',      component: ProjectDetailComponent,     resolve: { project: ProjectResolver }     children: [         {              path: ':projectId/edit',             component: EditProjectComponent,             resolve: { edit: EditProjectResolve }         }     ] } 

edit-project-component.ts

ngOnInit() {     this.edit = this.route.snapshot.data.edit; } 

2. You can bypass the child's resolver all together and access parent data from within the child component.

[...module.ts | ...component.ts]

{     path: 'project/:projectId',      component: ProjectDetailComponent,     resolve: { project: ProjectResolver }     children: [         {              path: ':projectId/edit',             component: EditProjectComponent         }     ] } 

edit-project-component.ts

ngOnInit() {     this.route.parent.data         .subscribe((data) => {             this.edit = data.edit;         }); } 
like image 120
Desmond Avatar answered Sep 21 '22 11:09

Desmond