例如此路由器
{
path: '/client',
component: ClientRootComponent,
children: [
{path: '', component: ClientListComponent},
{path: ':clientId', component: ClientOpenComponent, resolve: makeResolver(ClientOpenResolver)}
]
},
{
path: '**',
component: NotFoundComponent
}
会将 / client / 1234 和 / client / asdf 这两个网址传递给ClientOpenComponent。如何使 / client / asdf 与NotFound匹配并传递给NotFoundComponent?
答案 0 :(得分:4)
您可以将自定义匹配器传递到路线
import { defaultUrlMatcher } from '@angular/router/src/shared';
function digitsMatcher(segments: UrlSegment[], segmentGroup: UrlSegmentGroup, route: Route): UrlMatchResult | null {
const result = defaultUrlMatcher(segments, segmentGroup, route);
if (!result || !result.consumed || result.consumed.length < 1) {
return;
}
const re = /^\d+$/;
const match = re.exec(result.consumed[0].path);
if (match) {
return result;
}
return null;
}
{
path: '/client',
component: ClientRootComponent,
children: [
{path: '', component: ClientListComponent},
{path: ':clientId', component: ClientOpenComponent, resolve: makeResolver(ClientOpenResolver), matcher: digitsMatcher}
]
},
{
path: '**',
component: NotFoundComponent
}
代码未经过测试
另见