feat(router): add pathMatch property to replace terminal

This commit is contained in:
vsavkin
2016-06-27 20:10:36 -07:00
parent dc64e90ab9
commit fcfddbf79c
9 changed files with 56 additions and 39 deletions

View File

@ -149,7 +149,8 @@ function match(segment: UrlSegment, route: Route, paths: UrlPathWithParams[]): {
positionalParamSegments: {[k: string]: UrlPathWithParams}
} {
if (route.path === '') {
if (route.terminal && (segment.hasChildren() || paths.length > 0)) {
if ((route.terminal || route.pathMatch === 'full') &&
(segment.hasChildren() || paths.length > 0)) {
throw new NoMatch();
} else {
return {consumedPaths: [], lastChild: 0, positionalParamSegments: {}};
@ -286,7 +287,8 @@ function containsEmptyPathRedirects(
function emptyPathRedirect(
segment: UrlSegment, slicedPath: UrlPathWithParams[], r: Route): boolean {
if ((segment.hasChildren() || slicedPath.length > 0) && r.terminal) return false;
if ((segment.hasChildren() || slicedPath.length > 0) && (r.terminal || r.pathMatch === 'full'))
return false;
return r.path === '' && r.redirectTo !== undefined;
}

View File

@ -18,7 +18,12 @@ export type ResolveData = {
export interface Route {
path?: string;
/**
* @deprecated - use `pathMatch` instead
*/
terminal?: boolean;
pathMatch?: 'full'|'prefix';
component?: Type|string;
outlet?: string;
canActivate?: any[];
@ -53,4 +58,11 @@ function validateNode(route: Route): void {
throw new Error(
`Invalid route configuration of route '${route.path}': path cannot start with a slash`);
}
if (route.path === '' && route.redirectTo !== undefined &&
(route.terminal === undefined && route.pathMatch === undefined)) {
const exp =
`The default value of 'pathMatch' is 'prefix', but often the intent is to use 'full'.`;
throw new Error(
`Invalid route configuration of route '{path: "${route.path}", redirectTo: "${route.redirectTo}"}': please provide 'pathMatch'. ${exp}`);
}
}

View File

@ -53,7 +53,7 @@ export class RouterOutlet {
const snapshot = activatedRoute._futureSnapshot;
const component: any = <any>snapshot._routeConfig.component;
let factory;
let factory: ComponentFactory<any>;
try {
factory = typeof component === 'string' ?
snapshot._resolvedComponentFactory :

View File

@ -151,7 +151,8 @@ function processPathsWithParamsAgainstRoute(
function match(segment: UrlSegment, route: Route, paths: UrlPathWithParams[]) {
if (route.path === '') {
if (route.terminal && (segment.hasChildren() || paths.length > 0)) {
if ((route.terminal || route.pathMatch === 'full') &&
(segment.hasChildren() || paths.length > 0)) {
throw new NoMatch();
} else {
return {consumedPaths: [], lastChild: 0, parameters: {}};
@ -180,7 +181,8 @@ function match(segment: UrlSegment, route: Route, paths: UrlPathWithParams[]) {
currentIndex++;
}
if (route.terminal && (segment.hasChildren() || currentIndex < paths.length)) {
if ((route.terminal || route.pathMatch === 'full') &&
(segment.hasChildren() || currentIndex < paths.length)) {
throw new NoMatch();
}
@ -292,7 +294,8 @@ function containsEmptyPathMatches(
}
function emptyPathMatch(segment: UrlSegment, slicedPath: UrlPathWithParams[], r: Route): boolean {
if ((segment.hasChildren() || slicedPath.length > 0) && r.terminal) return false;
if ((segment.hasChildren() || slicedPath.length > 0) && (r.terminal || r.pathMatch === 'full'))
return false;
return r.path === '' && r.redirectTo === undefined;
}