feat(router): add support for wildcards
This commit is contained in:
parent
6f5e3f9390
commit
8836219b16
|
@ -47,9 +47,12 @@ function _constructSegment(componentResolver: ComponentResolver,
|
|||
matched: _MatchResult): Promise<TreeNode<RouteSegment>[]> {
|
||||
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<UrlSegment>): _MatchResu
|
|||
|
||||
function _matchWithParts(route: RouteMetadata, url: TreeNode<UrlSegment>): _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 = [];
|
||||
|
|
|
@ -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}),
|
||||
|
|
Loading…
Reference in New Issue