Overview
The Sushi Restaurant App uses Angular Router with Ionic’s routing enhancements to provide seamless navigation with lazy loading, route parameters, and preloading strategies.Routing Architecture
Root Routing Module
The application-level routing configuration:src/app/app-routing.module.ts
Key Configuration
RouterModule.forRoot()
RouterModule.forRoot()
The
forRoot() method configures the router at the application root level.Use
forRoot() only once in the root module. Feature modules use forChild().PreloadAllModules Strategy
PreloadAllModules Strategy
This strategy loads lazy modules in the background after the initial app load:Benefits:
- Faster initial load time
- Subsequent navigation is instant
- Better user experience
- Initial route loads first
- After app is stable, lazy modules load in background
- Modules are cached for instant access
The app loads the home page immediately, then preloads the detail page module
Default Route Redirect
Default Route Redirect
- Empty path redirects to
/home pathMatch: 'full'ensures exact match- User navigating to
/lands on/home
Lazy Loading Implementation
Dynamic Import Syntax
Lazy loading uses dynamicimport() statements:
- How it Works
- Bundle Analysis
- Benefits
- Bundle Splitting: Angular creates separate JavaScript bundles for each lazy module
- On-Demand Loading: Module is downloaded only when the route is accessed
- Caching: Once loaded, the module remains in memory
- Code Splitting: Reduces initial bundle size significantly
Feature Routing: Home Module
Feature-level routing configuration:src/app/home/home-routing.module.ts
Route Configuration Breakdown
Empty Path Route
Empty Path Route
- Renders
HomePageat/home - Empty path is relative to parent route (
home) - Eagerly loaded (component is in the same module)
/homeDynamic Route Parameter
Dynamic Route Parameter
:registroIdis a route parameter- Matches any value:
/home/1,/home/2, etc. - Lazy loads detail page module
/home/1→ Shows detail for item with ID “1”/home/2→ Shows detail for item with ID “2”
The colon (
:) prefix indicates a dynamic route parameterRouterModule.forChild()
RouterModule.forChild()
- Used in feature modules
- Registers routes as children of parent routes
- Can be imported multiple times (unlike
forRoot())
Route Parameters
Extracting Route Parameters
Access dynamic route parameters in components:src/app/home/detalle-registro/detalle-registro.page.ts
Parameter Extraction Methods
- Observable (Recommended)
- Snapshot (One-time)
- Comparison
- Reacts to parameter changes automatically
- Required if navigating within the same component
- Follows reactive programming patterns
Use this approach for dynamic updates when route parameters change
Programmatic Navigation
Using Router Service
Navigate programmatically from TypeScript code:Navigation Methods
Template Navigation
Navigate usingrouterLink directive in templates:
routerLink is preferred over (click) handlers for navigation—it’s more semantic and handles accessibility betterIonic Router Integration
IonicRouteStrategy
Ionic provides a custom route reuse strategy:src/app/app.module.ts
What is IonicRouteStrategy?
What is IonicRouteStrategy?
Ionic’s custom strategy for managing route lifecycle:Features:
- Preserves component state during navigation
- Optimizes mobile navigation animations
- Handles back button behavior
- Manages component caching
Without
IonicRouteStrategy, Angular’s default strategy would destroy components on every navigationComponent Lifecycle Benefits
Component Lifecycle Benefits
Example Scenario:
- User views list on home page
- User scrolls to item #10
- User taps item to view details
- User navigates back
- ✅ List is still scrolled to item #10 (state preserved)
Provides native-like behavior where back navigation restores previous state
ion-router-outlet
Ionic’s enhanced router outlet:src/app/app.component.html
- Animated page transitions
- Gesture-based navigation (swipe back)
- Stack-based navigation (maintains navigation history)
- Platform-specific animations (iOS vs Android)
ion-router-outlet extends Angular’s router-outlet with mobile-specific enhancementsNavigation Flow Diagram
Route Guards
While not implemented in the current app, route guards can protect routes:Available Guard Types
CanActivate
Determines if a route can be activated
CanDeactivate
Determines if user can leave a route (unsaved changes)
CanLoad
Prevents lazy loading of modules
Resolve
Pre-fetches data before activating route
Best Practices
Use Lazy Loading for All Features
Use Lazy Loading for All Features
Always Use PreloadAllModules in Production
Always Use PreloadAllModules in Production
Extract Route Parameters with paramMap
Extract Route Parameters with paramMap
Use Absolute Paths for Primary Navigation
Use Absolute Paths for Primary Navigation
Prefer routerLink Over Click Handlers
Prefer routerLink Over Click Handlers
Common Routing Patterns
Master-Detail Navigation
The app implements the master-detail pattern:- Master (
/home): List of all menu items - Detail (
/home/:id): Detailed view of single item
Nested Routes
The routing structure demonstrates nesting:Child routes inherit the parent path, creating a hierarchical URL structure
Next Steps
Architecture Overview
Learn about the application architecture patterns
Project Structure
Explore the directory layout and file organization