feat(router): add support for custom url matchers
Closes #12442 Closes #12772
This commit is contained in:
@ -6,6 +6,11 @@
|
||||
* found in the LICENSE file at https://angular.io/license
|
||||
*/
|
||||
|
||||
|
||||
import {Route, UrlMatchResult} from './config';
|
||||
import {UrlSegment, UrlSegmentGroup} from './url_tree';
|
||||
|
||||
|
||||
/**
|
||||
* @whatItDoes Name of the primary outlet.
|
||||
*
|
||||
@ -30,3 +35,35 @@ export class NavigationCancelingError extends Error {
|
||||
}
|
||||
toString(): string { return this.message; }
|
||||
}
|
||||
|
||||
export function defaultUrlMatcher(
|
||||
segments: UrlSegment[], segmentGroup: UrlSegmentGroup, route: Route): UrlMatchResult {
|
||||
const path = route.path;
|
||||
const parts = path.split('/');
|
||||
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 p = parts[i];
|
||||
const isPosParam = p.startsWith(':');
|
||||
|
||||
if (!isPosParam && p !== current.path) return null;
|
||||
if (isPosParam) {
|
||||
posParams[p.substring(1)] = current;
|
||||
}
|
||||
consumed.push(current);
|
||||
currentIndex++;
|
||||
}
|
||||
|
||||
if (route.pathMatch === 'full' &&
|
||||
(segmentGroup.hasChildren() || currentIndex < segments.length)) {
|
||||
return null;
|
||||
} else {
|
||||
return {consumed, posParams};
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user