有没有办法从中优化此代码
{
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
}
]
}
Run Code Online (Sandbox Code Playgroud)
这样的事情
{
path: 'access-requests',
canActivate: [AccessGuard],
component: AccessRequestsComponent,
children: [
{
path: '',
redirectTo: 'today',
pathMatch: 'full'
},
{
path: 'today | tomorrow | expired',
component: AccessRequestsComponent
}
]
}
Run Code Online (Sandbox Code Playgroud)
Bog*_*n D 13
您可以使用映射数组:
children: [
// excluding the other paths for brevity
...['today', 'tomorrow', 'expired'].map(path => ({
path,
component: AccessRequestsComponent
}))
]
Run Code Online (Sandbox Code Playgroud)
Cor*_*elC 10
您可以使用UrlMatcher属性.
{
path: 'access-requests',
canActivate: [AccessGuard],
component: AccessRequestsComponent,
children: [
{
path: '',
redirectTo: 'today',
pathMatch: 'full'
},
{
matcher: matcherFunction,
component: AccessRequestsComponent
}
]
}
Run Code Online (Sandbox Code Playgroud)
和
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;
}
Run Code Online (Sandbox Code Playgroud)
注意:未经测试的代码
| 归档时间: |
|
| 查看次数: |
6672 次 |
| 最近记录: |