Skip to content
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
fix(router): standardizeConfig inherit parent route outlet
Non primary routes will inherit their outlet name to their primary direct children
  • Loading branch information
matheo committed Aug 12, 2020
commit 4fcddc5691833535b86d4e9e76cca20c4167d84f
12 changes: 10 additions & 2 deletions packages/router/src/components/empty_outlet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

import {Component} from '@angular/core';
import {ActivatedRoute} from '../router_state';
import {PRIMARY_OUTLET} from '../shared';

/**
* This component is used internally within the router to be a placeholder when an empty
Expand All @@ -18,9 +19,16 @@ import {ActivatedRoute} from '../router_state';
* In order to render, there needs to be a component on this config, which will default
* to this `EmptyOutletComponent`.
*/
@Component({template: `<router-outlet lazy [name]="route.outlet"></router-outlet>`})
@Component({template: `<router-outlet lazy [name]="name"></router-outlet>`})
export class ɵEmptyOutletComponent {
constructor(public route: ActivatedRoute) {}
name!: string;

constructor(public route: ActivatedRoute) {
// lazy loaded children are handled as primary routes

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What's the motivation behind the distinction here? Don't lazy loaded children act the same way as regular children after they're loaded?

@matheo matheo Aug 12, 2020 •

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

With the previous attempt to modify standardizeConfig I realized that lazy loaded modules are supposed to use the primary outlet, no needing to know the parent outlet to load their children, so basically I will figure out the routing-hierarchy and what happens with the outlets in the same hierarchy level.

So I will test my use-case for different outlets in a lazy-loaded module.

this.name = route.snapshot.routeConfig?.loadChildren
? PRIMARY_OUTLET
: route.outlet;
}
}

export {ɵEmptyOutletComponent as EmptyOutletComponent};
2 changes: 1 addition & 1 deletion packages/router/src/operators/activate_routes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,7 @@ export class ActivateRoutes {
} else if (isDevMode() && console && console.warn) {
console.warn(
`A router outlet has not been instantiated during routes activation. URL Segment: '${
future.snapshot._urlSegment}'`);
context.route.outlet}: ${future.snapshot._urlSegment}'`);
}

this.activateChildRoutes(futureNode, null, context.children);
Expand Down
19 changes: 12 additions & 7 deletions packages/router/src/utils/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -105,11 +105,16 @@ function getFullPath(parentPath: string, currentRoute: Route): string {
/**
* Makes a copy of the config and adds any default required properties.
*/
export function standardizeConfig(r: Route): Route {
const children = r.children && r.children.map(standardizeConfig);
const c = children ? {...r, children} : {...r};
if (!c.component && (children || c.loadChildren) && (c.outlet && c.outlet !== PRIMARY_OUTLET)) {
c.component = EmptyOutletComponent;
}
return c;
export function standardizeConfig(i: Route): Route {
const children = i.children && i.children.map(c => {
if (i.outlet && i.outlet !== PRIMARY_OUTLET && (c.outlet === PRIMARY_OUTLET || c.outlet === undefined)) {
c.outlet = i.outlet;
}
return standardizeConfig(c);
});
const r = children ? {...i, children} : {...i};
if (!r.component && (children || r.loadChildren) && (r.outlet && r.outlet !== PRIMARY_OUTLET)) {
r.component = EmptyOutletComponent;
}
return r;
}