Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Angular child route on Lazy Loaded module does not load

Not sure do I do everything right. But the thing is: when I navigate to some child routes of component from lazy loaded module it simply does not load. It reloads home component from Lazy Loaded Module.

app-routing.component.ts

const routes: Routes = [
  {path: 'intel', loadChildren: () => import(`./intel/intel.module`).then(m => m.IntelModule)},
  {
    path: 'planet-detector',
    loadChildren: () => import('./planet-detector/planet-detector.module').then((m) => m.PlanetDetectorModule)
  },
  {path: '', redirectTo: 'space', pathMatch: 'full'},
  {path: '**', component: BlackHoleComponent}
];

@NgModule({
  imports: [RouterModule.forRoot(routes)],
  exports: [RouterModule]
})
export class AppRoutingModule { }

planet-detector-routing.module.ts

const routes: Routes = [
  {path: '', component: DetectorComponent, children: [
      { path: 'first', component: FirstChildComponent},
      { path: 'second', component: SecondChildComponent}
    ]}
];

@NgModule({
  imports: [RouterModule.forChild(routes)],
  exports: [RouterModule]
})
export class PlanetDetectorRoutingModule { }

So in the example as above when you put: 'http://localhost:4200/planet-detector/first' it loads DetectorComponent component instead of FirstChildComponent (url points to 'http://localhost:4200/planet-detector/first').

I noticed that when I change PlanetDetectorRoutingModule to:

const routes: Routes = [
  { path: '', component: DetectorComponent },
  { path: 'first', component: FirstChildComponent },
  { path: 'second', component: SecondChildComponent }
];

@NgModule({
  imports: [RouterModule.forChild(routes)],
  exports: [RouterModule]
})
export class PlanetDetectorRoutingModule { }

Then it works fine. And mb one more question. What are the benefits of these children route separation?

like image 940
Mateusz Korga Avatar asked Dec 15 '19 22:12

Mateusz Korga


People also ask

Does Angular support lazy loading?

New with Angular 8, loadChildren expects a function that uses the dynamic import syntax to import your lazy-loaded module only when it's needed. The dynamic import is promise-based and gives you access to the module, where the module's class can be called.

How do I know if lazy loading is working?

If you're not sure if lazy loading is working correctly, you can open the Chrome DevTools and check that the images are not loaded until the scroll.

How do you use a lazy load module without routing?

define where we want to load our component in the template with the ng-template tag, define its view query through ViewChild decorator, which gives us access to the DOM and defines the container to which the component will be added, finally, dynamic import the component and add it to the container.

How lazy loading works in Angular?

Lazy loading is a technique in Angular that allows you to load JavaScript components asynchronously when a specific route is activated. It improves the speed of the application load time by splitting the application into several bundles. When the user navigates through the app, the bundles are loaded as required.


1 Answers

When routes are declared in the children property, it means that they should be rendered as a child of that component.

So for that route to be rendered, there needs to be a <router-outlet> in the DetectorComponent.

Routes listed in children follow this rule:

There are important differences in the way the router treats these child routes.

The router displays the components of these routes in the RouterOutlet of the ParentComponent, not in the RouterOutlet of the AppComponent shell.

like image 171
C_Ogoo Avatar answered Nov 03 '22 00:11

C_Ogoo