How can I get a Lazy Module to access a named outlet in Angular?

Viewed 410

Problem: I have a lazy module that wants to use the popup router in my app.component.html.

I have the following in my app component template:

src/app/app.component.html

<!-- Primary Router -->
<router-outlet id="main-router"></router-outlet>

<!-- Popup Router -->
<router-outlet name="app-popup"></router-outlet> <!-- <--------- -->

My App Module and it's routes:

/src/app/app-routing.module.ts

const appRoutes: Routes = [
  { path: 'lazy',  loadChildren: 'app/lazy/lazy.module#LazyModule' }, // <--------
];
@NgModule({
  imports: [
    CommonModule,
    RouterModule.forRoot(appRoutes),
  ],
  exports: [
    RouterModule
  ],
})
export class AppRoutingModule { }

/src/app/app.module.ts

@NgModule({
  declarations: [AppComponent],
  imports: [
    BrowserModule,
    AppRoutingModule
  ],
  bootstrap: [AppComponent]
})
export class AppModule {}

My Lazy Module and it's routes:

/src/app/lazy/lazy.module.ts

const lazyRoutes: Routes = [
  { path: 'lazy-popup', outlet: 'app-popup', component: LazyPopupComponent },
  { path: '', component: LazyComponent }
];
@NgModule({
  imports: [
    RouterModule.forChild(lazyRoutes)
  ],
  exports: [
      RouterModule
  ]
})
export class LazyRoutingModule {}

/src/app/lazy/lazy.module.ts

@NgModule({
  imports: [
    CommonModule,
    AppSharedModule,
    LazyRoutingModule
  ],
  declarations: [
    LazyComponent,
    LazyPopupComponent
  ],
  exports: [
    LazyPopupComponent,
    LazyRoutingModule,
  ]
})
export class LazyModule {}

I can't seem to get it to work as I get this error:

core.es5.js:1020 ERROR Error: Uncaught (in promise): Error: Cannot match 
any routes. URL Segment: 'lazy-popup'.

Note that this works perfectly though if I import the LazyModule in AppModule below:

@NgModule({
  declarations: [AppComponent],
  imports: [
    BrowserModule,
    AppRoutingModule,
    LazyModule // <-- This routes works now.. But.. this should be lazy.. : (
  ],
  bootstrap: [AppComponent]
})
export class AppModule {}

Although this defeats the purpose of it being a lazy loaded module. So how can I get the lazy loaded module use the secondary router-outlet named 'app-popup' that exist in the app components html template?

Update:

I navigate to the route like this:

/src/app/lazy/lazy.component.html

<button [routerLink]=" [ '', { outlets: { 'app-popup': ['lazy-popup'] } }] "></button>
0 Answers
Related