Issue
Is there a way to optimize this code from this
{
path: 'access-requests',
canActivate: [AccessGuard],
component: AccessRequestsComponent,
children: [
{
path: '',
redirectTo: 'today',
pathMatch: 'full'
},
{
path: 'today',
component: AccessRequestsComponent
},
{
path: 'tomorrow',
component: AccessRequestsComponent
},
{
path: 'expired',
component: AccessRequestsComponent
}
]
}
to something like this
{
path: 'access-requests',
canActivate: [AccessGuard],
component: AccessRequestsComponent,
children: [
{
path: '',
redirectTo: 'today',
pathMatch: 'full'
},
{
path: 'today | tomorrow | expired',
component: AccessRequestsComponent
}
]
}
Solution
You can use the UrlMatcher property.
{
path: 'access-requests',
canActivate: [AccessGuard],
component: AccessRequestsComponent,
children: [
{
path: '',
redirectTo: 'today',
pathMatch: 'full'
},
{
matcher: matcherFunction,
component: AccessRequestsComponent
}
]
}
And
export function matcherFunction(url: UrlSegment[]) {
if (url.length == 1) {
const path = url[0].path;
if(path.startsWith('today')
|| path.startsWith('tomorrow')
|| path.startsWith('expired')){
return {consumed: url};
}
}
return null;
}
Note: Untested code