Here's how I managed to do what you want to do. The idea is that, since the login component is in a lazy module, since lazy modules are loaded by the router when navigating to some routes, you need to navigate, inside the modal, to a route that loads the lazy module and shows the login component. And this can be done using a named router outlet. I'm not very experienced with named router outlets, so there might be some things to refine, but it seems to work.
So, assuming you have a lazy module LoginModule
, with an empty-path route displaying the login component, here's how the root module's routes can be defined:
export const ROUTES: Routes = [
{
path: '',
component: HomeComponent
},
{
path: 'login',
loadChildren: './login/login.module#LoginModule'
},
{
path: 'modal-login',
component: ModalLoginShellComponent,
outlet: 'modal',
children: [
{
path: '',
loadChildren: './login/login.module#LoginModule'
}
]
}
];
The home component would have a link allowing to open a ModalLoginComponent inside a modal (as shown in the ng-bootstrap example). This ModalLoginComponent template would look like this:
<div class="modal-header">
<h4 class="modal-title">Hi there!</h4>
<button type="button" class="close" aria-label="Close" (click)="activeModal.dismiss('Cross click')">
<span aria-hidden="true">×</span>
</button>
</div>
<div class="modal-body">
<router-outlet name="modal"></router-outlet>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-outline-dark" (click)="activeModal.close('Close click')">Close</button>
</div>
The important part being
<router-outlet name="modal"></router-outlet>
It allows, inside the modal, to navigate to a route, and in particular, to a route that would load the lazy module.
The code of the ModalLoginComponent would have the following ngOnInit(), which would trigger the navigation:
ngOnInit() {
this.router.navigate([{outlets: {'modal': ['modal-login']}}]);
}
This would thus load the ModalLoginShellComponent and its default lazy-loaded child route inside the body of the modal. The ModalLoginShellComponent is a stupid component doing nothing, and just having, as its template
<router-outlet></router-outlet>