Skip to content

feat(router): Allow loadChildren to return a Route array#45700

Closed
atscott wants to merge 1 commit intoangular:masterfrom
atscott:loadChildrenRoutes
Closed

feat(router): Allow loadChildren to return a Route array#45700
atscott wants to merge 1 commit intoangular:masterfrom
atscott:loadChildrenRoutes

Conversation

@atscott
Copy link
Contributor

@atscott atscott commented Apr 20, 2022

This commit expands the LoadChildrenCallback to accept returning Routes
in addition to the existing NgModule type. In addition, it adds a
check to ensure these loaded routes all use standalone components.
The components must be standalone because if they were not,
we would not have the required NgModule which the component is declared in.

Existing API:

{path: 'lazy/route', loadChildren: import('./lazy').then(m => m.LazyModule)}

@NgModule({
  imports: [
    ExtraCmpModule,
    RouterModule.forChild([
      {path: 'extra/route', component: ExtraCmp},
    ]),
  ],
})
export class LazyModule {}

The new API for lazy loading route configs with standalone components
(no NgModule) is to expand loadChildren to allow returning simply a Routes array.

// parent.ts
{
  path: 'parent',
  loadChildren: () => import('./children').then(m => m.ROUTES),
}

// children.ts
export const ROUTES: Route[] = [
  {path: 'child', component: ChildCmp},
];

Note that this includes minimal documentation updates. We need to
include a holistic update to the documentation for standalone components
in the future that includes this feature.

@atscott atscott added area: router target: minor This PR is targeted for the next minor release labels Apr 20, 2022
@ngbot ngbot bot added this to the Backlog milestone Apr 20, 2022
@atscott atscott force-pushed the loadChildrenRoutes branch 2 times, most recently from 5a0cf43 to 37dd312 Compare April 20, 2022 19:56
Copy link
Contributor

@AndrewKushnir AndrewKushnir left a comment

Choose a reason for hiding this comment

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

👍

@atscott atscott force-pushed the loadChildrenRoutes branch 2 times, most recently from 8005af8 to ea2aef2 Compare April 20, 2022 21:24
@atscott atscott force-pushed the loadChildrenRoutes branch from ea2aef2 to b6ada11 Compare April 20, 2022 23:03
@atscott atscott marked this pull request as ready for review April 20, 2022 23:07
Copy link
Contributor

@jessicajaniuk jessicajaniuk left a comment

Choose a reason for hiding this comment

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

reviewed-for: public-api

@atscott atscott force-pushed the loadChildrenRoutes branch from b6ada11 to d826217 Compare April 21, 2022 18:51
Copy link
Member

@alxhub alxhub left a comment

Choose a reason for hiding this comment

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

Reviewed-for: public-api, fw-core, fw-router

Copy link
Contributor

@AndrewKushnir AndrewKushnir left a comment

Choose a reason for hiding this comment

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

Looks great 👍

@pullapprove pullapprove bot requested a review from AndrewKushnir April 22, 2022 00:00
Copy link
Contributor

@AndrewKushnir AndrewKushnir left a comment

Choose a reason for hiding this comment

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

Reviewed-for: public-api

@AndrewKushnir AndrewKushnir removed the request for review from dylhunn April 22, 2022 00:00
This commit expands the `LoadChildrenCallback` to accept returning `Routes`
in addition to the existing `NgModule` type. In addition, it adds a
check to ensure these loaded routes all use standalone components.
The components must be standalone because if they were not,
we would not have the required `NgModule` which the component is declared in.

Existing API:
```
{path: 'lazy/route', loadChildren: import('./lazy').then(m => m.LazyModule)}

@NgModule({
  imports: [
    ExtraCmpModule,
    RouterModule.forChild([
      {path: 'extra/route', component: ExtraCmp},
    ]),
  ],
})
export class LazyModule {}
```

The new API for lazy loading route configs with standalone components
(no NgModule) is to expand `loadChildren` to allow returning simply a `Routes` array.

```
// parent.ts
{
  path: 'parent',
  loadChildren: () => import('./children').then(m => m.ROUTES),
}

// children.ts
export const ROUTES: Route[] = [
  {path: 'child', component: ChildCmp},
];
```

Note that this includes minimal documentation updates. We need to
include a holistic update to the documentation for standalone components
in the future that includes this feature.
@atscott atscott force-pushed the loadChildrenRoutes branch from d826217 to 374852c Compare April 22, 2022 01:03
@atscott
Copy link
Contributor Author

atscott commented Apr 22, 2022

presubmit

merge assistance: requires an update to the g3 patch: http://cl/443671599

@atscott atscott added action: merge The PR is ready for merge by the caretaker merge: caretaker note Alert the caretaker performing the merge to check the PR for an out of normal action needed or note labels Apr 22, 2022
@atscott
Copy link
Contributor Author

atscott commented Apr 22, 2022

This PR was merged into the repository by commit 4962a4a.

@atscott atscott closed this in 4962a4a Apr 22, 2022
@angular-automatic-lock-bot
Copy link

This issue has been automatically locked due to inactivity.
Please file a new issue if you are encountering a similar or related problem.

Read more about our automatic conversation locking policy.

This action has been performed automatically by a bot.

@angular-automatic-lock-bot angular-automatic-lock-bot bot locked and limited conversation to collaborators May 23, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

action: merge The PR is ready for merge by the caretaker area: router merge: caretaker note Alert the caretaker performing the merge to check the PR for an out of normal action needed or note target: minor This PR is targeted for the next minor release

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants