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
}
]
}
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
You can use a mapped array:
children: [
// excluding the other paths for brevity
...['today', 'tomorrow', 'expired'].map(path => ({
path,
component: AccessRequestsComponent
}))
]
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With