🚀 LEVEL UP TO SENIOR:Unlock 500+ Advanced Practical Challenges & Exercises.
🎓 COURSERA PARTNER:Earn professional Google, Meta, and IBM certificates to supercharge your resume.
HTML MASTER CLASS /// LEARN TAGS /// BUILD STRUCTURE /// SEMANTIC WEB /// HTML MASTER CLASS /// LEARN TAGS ///

Child Routes & Lazy Loading in Angular

Learn about Child Routes & Lazy Loading in this comprehensive Angular tutorial. Learn how to organize your application into a logical hierarchy with child routes and optimize performance with lazy loading.

Total XP: 0|💻 angular XP: 0

Skill Matrix

UNLOCK NODES BY LEARNING NEW TAGS.

Select an unlocked node to view details root

🚀 LEVEL UP TO SENIOR:Unlock 500+ Advanced Practical Challenges & Exercises.
🎓 COURSERA PARTNER:Earn professional Google, Meta, and IBM certificates to supercharge your resume.

As applications grow in complexity, flat routing structures become insufficient. Advanced routing techniques are essential for maintaining performance and organization.

1Nested Navigation Logic

Child routes are more than just a way to group URLs. They allow you to build persistent layouts. When navigating between children of the same parent, the parent component is NOT destroyed. This means you can have a persistent sidebar, header, or state in the parent while only the content inside the child's <router-outlet> changes. This is the key to building complex, stateful dashboards.

2Performance at Scale

Lazy Loading is a 'must-have' for large apps. By splitting your application into feature modules and only loading them when needed, you drastically reduce the 'Time to Interactive'. Angular handles the complexity of fetching the JavaScript chunks behind the scenes, providing a smooth experience for the user while keeping the initial download size minimal.

3Step-by-Step Breakdown

As your app grows, flat routes become hard to manage. Child routes allow you to nest views inside other views.

Think of a Dashboard. It might have 'profile' and 'settings' sections. We can nest these under a single '/dashboard' path.

To render these children, the parent (DashboardComponent) MUST have its own <router-outlet> in its template.

Checkpoint: Which property in a route object is used to define nested routes?

  • nested
  • children

Now let's talk speed. 'Lazy Loading' prevents your app from downloading every single module at once.

We use 'loadChildren' to tell Angular: 'Download this module ONLY when the user clicks the link'.

Checkpoint: What is the main benefit of using Lazy Loading in an Angular application?

  • It makes the code unreadable
  • It reduces initial load time by loading features on demand

Combining nested routes and lazy loading is how professional Angular developers build enterprise-scale applications.

Ready for user input? Next, we'll start our journey into Angular Forms!

Level Up 🚀

Advanced cheat sheets, SEO tricks, and interview prep for this topic.

Browser Support

ChromeSupported

Fully supported.

FirefoxSupported

Fully supported.

SafariSupported

Fully supported.

EdgeSupported

Fully supported.

Accessibility (A11y)

1Route Transitions Should Move Focus, Not Leave It Stranded

By default, navigating to a child route doesn't move keyboard focus anywhere — sighted keyboard users and screen reader users are left focused on whatever link they clicked, with no indication the page changed. Programmatically move focus to the new view's heading after navigation.

2Announce Route Changes to Screen Reader Users

Because Angular's router swaps content without a full page reload, screen readers don't get the same natural 'new page loaded' cue they would from a traditional navigation — use an `aria-live` region updated with the new route's title to close that gap.

SEO Implications

  • 1

    Lazy-Loaded Modules Must Still Be Reachable by Server-Side Rendering

    If a lazy-loaded feature module contains content that should be indexed, ensure your Angular Universal setup actually resolves and renders those routes server-side — lazy loading optimizes client bundle size, but shouldn't accidentally exclude a route from SSR.

  • 2

    Deep Child Routes Need Their Own Title and Meta Tags

    Every meaningfully distinct child route should call Angular's `Title`/`Meta` services with content specific to that route — inheriting the parent route's generic title for every nested child wastes an opportunity for more precise search snippets.

Best Practices

Lazy-Load Feature Modules by Route, Not Just by File Size

Split at meaningful feature boundaries (e.g., an admin section a typical user never visits) rather than arbitrarily — this keeps the initial bundle small for the common path while deferring genuinely optional code.

Use a Resolver to Prevent Rendering a Route Before Its Data Is Ready

A `Resolve` guard fetches required data before the route activates, avoiding a flash of empty or partially-loaded UI that a plain `ngOnInit` fetch inside the component would produce.

Frequent Bugs

THE BUG

A lazy-loaded module's routes work when navigated to directly but fail when linked from elsewhere in the app.

THE FIX

Check that the parent route's lazy-loading configuration (`loadChildren`) is correctly registered and that the child module's own routing exports the expected routes — a common cause is forgetting to import `RouterModule.forChild()` in the lazy-loaded feature module.

THE BUG

After navigating between child routes, keyboard focus and screen reader announcements don't reflect the page change.

THE FIX

Angular's router doesn't move focus automatically on navigation, unlike a full page load. Subscribe to router events and programmatically call `.focus()` on the new view's main heading, and update an `aria-live` region with the new page title.

Real-World Examples

Lazy-Loaded Admin Feature Module

A large app defers loading its entire admin section until a user actually navigates there, keeping the initial bundle small for the vast majority of users who never visit it.

const routes: Routes = [
  { path: 'admin', loadChildren: () => import('./admin/admin.module').then(m => m.AdminModule) }
];

Interview Prep

?Frequently Asked Questions

Pascual Vila

Pascual Vila

Frontend Instructor // Code Syllabus

Common Pitfalls & Errors

The Error //

Memory leaks from unclosed Subscriptions

// Wrong ngOnInit() { this.dataService.getData().subscribe(data => this.data = data); } // Correct ngOnInit() { this.sub = this.dataService.getData().subscribe(data => this.data = data); } ngOnDestroy() { if (this.sub) this.sub.unsubscribe(); }

The Solution //

When subscribing to Observables in a component, always unsubscribe in the ngOnDestroy hook to prevent memory leaks.

The Error //

Directly manipulating the DOM

// Wrong document.getElementById('my-el').style.color = 'red'; // Correct @ViewChild('myEl') myEl: ElementRef; this.renderer.setStyle(this.myEl.nativeElement, 'color', 'red');

The Solution //

Avoid using document.getElementById or native DOM APIs. Use Angular's templating, bindings, and tools like Renderer2 or ViewChild.

Lesson Glossary

[01]Child Route

A route defined within the 'children' array of a parent route, allowing for nested views.

Code Preview
children

[02]Lazy Loading

A design pattern that deferes the loading of a module until the moment it is needed.

Code Preview
loadChildren

[03]Dynamic Import

A JavaScript feature used by loadChildren to fetch a module file over the network at runtime.

Code Preview
import()

[04]Bundle

The single file (or group of files) that contains all the compiled code for your application.

Code Preview
main.js

[05]Feature Module

An Angular module used to group related features together, often for the purpose of lazy loading.

Code Preview
@NgModule

[06]Time to Interactive

A performance metric that measures how long it takes for a page to become fully interactive for the user.

Code Preview
TTI

Continue Learning