From 8836219b16552e6a420ee65c33caa0fc5d388032 Mon Sep 17 00:00:00 2001 From: vsavkin Date: Sat, 30 Apr 2016 18:08:26 -0700 Subject: [PATCH] feat(router): add support for wildcards --- modules/angular2/src/alt_router/recognize.ts | 14 +++++++++++--- .../test/alt_router/recognize_spec.ts | 19 +++++++++++++++++++ 2 files changed, 30 insertions(+), 3 deletions(-) diff --git a/modules/angular2/src/alt_router/recognize.ts b/modules/angular2/src/alt_router/recognize.ts index e5c783076a..06d91362f1 100644 --- a/modules/angular2/src/alt_router/recognize.ts +++ b/modules/angular2/src/alt_router/recognize.ts @@ -47,9 +47,12 @@ function _constructSegment(componentResolver: ComponentResolver, matched: _MatchResult): Promise[]> { return componentResolver.resolveComponent(matched.component) .then(factory => { - let urlOutlet = matched.consumedUrlSegments[0].outlet; - let segment = new RouteSegment(matched.consumedUrlSegments, matched.parameters, - isBlank(urlOutlet) ? DEFAULT_OUTLET_NAME : urlOutlet, + let urlOutlet = matched.consumedUrlSegments.length === 0 || + isBlank(matched.consumedUrlSegments[0].outlet) ? + DEFAULT_OUTLET_NAME : + matched.consumedUrlSegments[0].outlet; + + let segment = new RouteSegment(matched.consumedUrlSegments, matched.parameters, urlOutlet, matched.component, factory); if (matched.leftOverUrl.length > 0) { @@ -102,6 +105,11 @@ function _match(metadata: RoutesMetadata, url: TreeNode): _MatchResu function _matchWithParts(route: RouteMetadata, url: TreeNode): _MatchResult { let path = route.path.startsWith("/") ? route.path.substring(1) : route.path; + + if (path == "*") { + return new _MatchResult(route.component, [], null, [], []); + } + let parts = path.split("/"); let positionalParams = {}; let consumedUrlSegments = []; diff --git a/modules/angular2/test/alt_router/recognize_spec.ts b/modules/angular2/test/alt_router/recognize_spec.ts index 888966045a..4c92cf4ace 100644 --- a/modules/angular2/test/alt_router/recognize_spec.ts +++ b/modules/angular2/test/alt_router/recognize_spec.ts @@ -149,6 +149,19 @@ export function main() { }); })); + it('should match a wildcard', + inject([AsyncTestCompleter, ComponentResolver], (async, resolver) => { + recognize(resolver, ComponentG, tree("a;aa=1/b;bb=2")) + .then(r => { + let c = r.children(r.root); + expect(c.length).toEqual(1); + expect(stringifyUrl(c[0].urlSegments)).toEqual([]); + expect(c[0].parameters).toEqual(null); + + async.done(); + }); + })); + it('should error when no matching routes', inject([AsyncTestCompleter, ComponentResolver], (async, resolver) => { recognize(resolver, ComponentA, tree("invalid")) @@ -214,6 +227,12 @@ class ComponentC { class ComponentB { } +@Component({selector: 'g', template: 't'}) +@Routes( + [new Route({path: "d", component: ComponentD}), new Route({path: "*", component: ComponentE})]) +class ComponentG { +} + @Component({selector: 'a', template: 't'}) @Routes([ new Route({path: "b/:b", component: ComponentB}),