I have the following routing paths for a module of my Angular app:
@NgModule({
imports: [
RouterModule.forChild([
{
path: 'documents',
data: { myObject: MyConstants.OPTION_ONE },
children: [
{
path: ':ID_DOC',
children: [
{ path: 'edit', component: EditDocumentComponent },
{ path: '', component: DocumentDetailsComponent },
]
},
{ path: 'add', component: AddDocumentComponent },
{ path: '', component: DocumentsListComponent }
]
}
])
],
exports: [
RouterModule
]
})
export class DocumentsManagementRoutingModule {
}
As you can see I use data
property to pass some data to every path in "documents", so I can get it from any of the Components declared in the routing paths:
For example here is how I get data in DocumentDetailsComponent
:
export class DocumentDetailsComponent implements OnDestroy {
private obsData: Subscription;
private option: any;
constructor(private route: ActivatedRoute) {
this.obsData = this.route.data.subscribe(data => {
this.option = data['myObject'];
});
}
ngOnDestroy(): void {
this.obsData.unsubscribe();
}
}
Now I changed the routing structure of the entire app and I call the above module from other modules, in lazy loading, using loadChildren
attribute. And I pass data in the same way:
@NgModule({
imports: [
RouterModule.forChild([
{
path: 'users',
loadChildren: 'app/documents/documents.module#DocumentsModule',
data: { myObject: MyConstants.OPTION_ONE }},
{
path: ':ID_USER',
children: [
{ path: 'edit', component: EditUserComponent },
{ path: '', component: UserDetailsComponent },
]
},
{ path: 'add', component: AddUserComponent },
{ path: '', component: UserListComponent }
])
],
exports: [
RouterModule
]
})
export class UsersManagementRoutingModule {
}
And I did the same for all the other modules that calls DocumentsManagementRoutingModule
, changing myObject
property to MyConstants.OPTION_TWO
, MyConstants.OPTION_THREE
and so on, according to my needs.
Then, since I don't know the module and its related path that calls my lazy loading module, how to get data
property from the caller module?
r.url.lastIndexOf("/"));
illegal. – LangillUsersManagementRoutingModule
– LangillUsersManagementRoutingModule
– LangillloadChildren
andchildren
should not be used simultaneously. Define your child routes inDocumentsModule
and then remove thechildren
field from the'users'
route withinUsersManagementRoutingModule
. Unless there's a typo and you meant to have two separate routes here, one withloadChildren
and one without... – McmahonDocumentsManagementRoutingModule
? Are they different routes (e.g.,'/users/documents'
,'/pages/documents'
, etc)? What does your route hierarchy look like? You can try using Augury (augury.angular.io) to get a nice route diagram, too. – Mcmahonroute.params
. Make some comparison. – Langillroute.params
looks for:ID
paths, notdata
params. – Cooksroute.params
looks for:ID
,route.queryParams
looks for query param in the url,route.data
looks fordata
property on a router path. Here is a plunker, as you can see the last child can't seedata
: plnkr.co/edit/GpNQxfWVi4BrF5MNc6Ml. Is it right that I have to useroute.parent.params
to make it working? Androute.parent.parent.params
if I add more children under last child? – Cooks