-
-
Save tytskyi/2e4ae91fd8f13164a08c70046f1b6f43 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Copyright (c) Matan Shukry | |
* All rights reserved. | |
*/ | |
import { UrlSegment, UrlSegmentGroup, Route } from '@angular/router'; | |
// export type UrlMatchResult = { | |
// consumed: UrlSegment[]; posParams?: { [name: string]: UrlSegment }; | |
// }; | |
export function ComplexUrlMatcher(paramName: string, regex: RegExp) { | |
return ( | |
segments: UrlSegment[], | |
segmentGroup: UrlSegmentGroup, | |
route: Route) => { | |
const parts = [regex]; | |
const posParams: { [key: string]: UrlSegment } = {}; | |
const consumed: UrlSegment[] = []; | |
let currentIndex = 0; | |
for (let i = 0; i < parts.length; ++i) { | |
if (currentIndex >= segments.length) { | |
return null; | |
} | |
const current = segments[currentIndex]; | |
const part = parts[i]; | |
if (!part.test(current.path)) { | |
return null; | |
} | |
posParams[paramName] = current; | |
consumed.push(current); | |
currentIndex++; | |
} | |
if (route.pathMatch === 'full' && | |
(segmentGroup.hasChildren() || currentIndex < segments.length)) { | |
return null; | |
} | |
return { consumed, posParams }; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Copyright (c) Matan Shukry | |
* All rights reserved. | |
*/ | |
export const UserRoutes: Routes = [ | |
{ | |
path: 'users', | |
component: UserComponent, | |
children: [ | |
{ | |
path: '', | |
component: UserListComponent | |
}, | |
{ | |
matcher: ComplexUrlMatcher("id", /[0-9]+/), | |
component: UserItemComponent | |
}, | |
] | |
} | |
]; | |
@NgModule({ | |
imports: [RouterModule.forChild(UserRoutes)], | |
exports: [RouterModule] | |
}) | |
export class UserRoutingModule { } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This doesn't work with AOT.
Here is the error message produced when compiling with AOT
Error encountered resolving symbol values statically. Expression form not supported