I'm trying to understand how I'm supposed to organize multiple routing files and make them all play nicely together.
My desired routes look like this:
/
/forms
/forms/:id
/forms/named-form (named-form2, named-form3, etc.)
/forms/named-form(2,3,etc)/admin
/forms/named-form(2,3,etc)/admin/(several options here)
My desired file organization:
/app.module
/app.routes
/app.component <--Router Outlet
/forms/forms.module
/forms/forms.routes
/forms/forms.component <--Router Outlet for displaying Form Creation, Details, Settings, etc.
/forms/named-form/named-form.module
/forms/named-form/named-form.routes
/forms/named-form/named-form.component <--Router Outlet for displaying Form or Admin section
/forms/named-form/admin/admin.module
/forms/named-form/admin/admin.routing
/forms/named-form/admin/admin.component <--Router Outlet for various admin pages
The way I have things now:
/forms.routing.ts
const formsRoutes: Routes = [
{
path: 'forms',
component: FormsComponent,
children: [
{ path: '', component: FormsListComponent },
{ path: ':id', component: FormsListComponent }
]
}
];
export const formsRouting = RouterModule.forChild(formsRoutes);
/forms/named-form.routing.ts
const namedFormRoutes: Routes = [
{
path: 'forms/named-form',
component: NamedFormComponent,
children: [
{
path: '',
component: NamedFormFormComponent,
},
{
path: 'confirmation',
component: NamedFormConfirmationComponent
}
]
}
];
/forms/named-form/admin.routes.ts
const namedFormAdminRoutes: Routes = [
{
path: 'forms/named-form/admin',
component: NamedFormAdminComponent,
children: [
{
path: '',
component: ManageDataComponent,
},
{
path: 'manage-data',
component: ManageDataComponent
},
{
path: 'settings',
component: SettingsComponent
},
{
path: 'import',
component: ImportDataComponent
}
]
}
];
This mostly works but there are a few problems I have with it. I wish I didn't have to specify the full paths the deeper I go in the nested routes. For instance the admin path is forms/named-form/admin
but I'd like it to already know it's a child of named-form
. This is one of the things that leads me to think I'm doing something wrong.
I also need to be able to do things in my named-form.component
file that will effect the admin.component
but the named-form.component
doesn't currently get touched if I navigate to forms/named-form/admin
. It ends up using the primary app.component
router-outlet instead of the named-form.component
router-outlet.
I can include a path for admin
inside the named-form.routes
file but then I don't know how to setup the admin children routes without also having to import all the main admin child components as well which makes it less modular.
I'm not sure if seeing my modules is necessary, but if so I'll add them in.
How can I do this better? Thanks for looking.