Skip to content

Commit

Permalink
fix(router): fix wildcard route with lazy loaded module (again) (#18139)
Browse files Browse the repository at this point in the history
Closes #13848

Description:
We doesn't handle children of wildcard route properly link. It's always an empty array.

Created from #13851

PR Close #18139
  • Loading branch information
jasonaden authored and IgorMinar committed Dec 22, 2017
1 parent 07b81ae commit 5ba1cf1
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 15 deletions.
36 changes: 23 additions & 13 deletions packages/router/src/recognize.ts
Original file line number Diff line number Diff line change
Expand Up @@ -111,29 +111,33 @@ class Recognizer {

if ((route.outlet || PRIMARY_OUTLET) !== outlet) throw new NoMatch();

let snapshot: ActivatedRouteSnapshot;
let consumedSegments: UrlSegment[] = [];
let rawSlicedSegments: UrlSegment[] = [];

if (route.path === '**') {
const params = segments.length > 0 ? last(segments) !.parameters : {};
const snapshot = new ActivatedRouteSnapshot(
snapshot = new ActivatedRouteSnapshot(
segments, params, Object.freeze(this.urlTree.queryParams), this.urlTree.fragment !,
getData(route), outlet, route.component !, route, getSourceSegmentGroup(rawSegment),
getPathIndexShift(rawSegment) + segments.length, getResolve(route));
return [new TreeNode<ActivatedRouteSnapshot>(snapshot, [])];
} else {
const result: MatchResult = match(rawSegment, route, segments);
consumedSegments = result.consumedSegments;
rawSlicedSegments = segments.slice(result.lastChild);

snapshot = new ActivatedRouteSnapshot(
consumedSegments, result.parameters, Object.freeze(this.urlTree.queryParams),
this.urlTree.fragment !, getData(route), outlet, route.component !, route,
getSourceSegmentGroup(rawSegment),
getPathIndexShift(rawSegment) + consumedSegments.length, getResolve(route));
}

const {consumedSegments, parameters, lastChild} = match(rawSegment, route, segments);
const rawSlicedSegments = segments.slice(lastChild);
const childConfig = getChildConfig(route);
const childConfig: Route[] = getChildConfig(route);

const {segmentGroup, slicedSegments} =
split(rawSegment, consumedSegments, rawSlicedSegments, childConfig);

const snapshot = new ActivatedRouteSnapshot(
consumedSegments, parameters, Object.freeze(this.urlTree.queryParams),
this.urlTree.fragment !, getData(route), outlet, route.component !, route,
getSourceSegmentGroup(rawSegment), getPathIndexShift(rawSegment) + consumedSegments.length,
getResolve(route));


if (slicedSegments.length === 0 && segmentGroup.hasChildren()) {
const children = this.processChildren(childConfig, segmentGroup);
return [new TreeNode<ActivatedRouteSnapshot>(snapshot, children)];
Expand Down Expand Up @@ -168,7 +172,13 @@ function getChildConfig(route: Route): Route[] {
return [];
}

function match(segmentGroup: UrlSegmentGroup, route: Route, segments: UrlSegment[]) {
interface MatchResult {
consumedSegments: UrlSegment[];
lastChild: number;
parameters: any;
}

function match(segmentGroup: UrlSegmentGroup, route: Route, segments: UrlSegment[]): MatchResult {
if (route.path === '') {
if (route.pathMatch === 'full' && (segmentGroup.hasChildren() || segments.length > 0)) {
throw new NoMatch();
Expand Down
5 changes: 3 additions & 2 deletions packages/router/test/integration.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3470,10 +3470,10 @@ describe('Integration', () => {
declarations: [LazyLoadedComponent],
imports: [RouterModule.forChild([{path: '', component: LazyLoadedComponent}])],
})
class LoadedModule {
class LazyLoadedModule {
}

loader.stubbedModules = {lazy: LoadedModule};
loader.stubbedModules = {lazy: LazyLoadedModule};
const fixture = createRoot(router, RootCmp);

router.resetConfig([{path: '**', loadChildren: 'lazy'}]);
Expand All @@ -3482,6 +3482,7 @@ describe('Integration', () => {
advance(fixture);

expect(location.path()).toEqual('/lazy');
expect(fixture.nativeElement).toHaveText('lazy-loaded');
})));

describe('preloading', () => {
Expand Down

0 comments on commit 5ba1cf1

Please sign in to comment.