Angular Router

Book description

From Angular core team member and creator of the router

About This Book

  • Written by the creator of the Angular router, giving you the best information straight from the source
  • Get full coverage of the entire Angular Router library and understand exactly how every command works
  • Essential for all serious users of Angular who need to manage states within their applications

Who This Book Is For

To get the most from this book, you should already have a good understanding of Angular and general web development.

What You Will Learn

  • Understand the role of the Angular router and how to make the most of it
  • Build and parse complex URLs
  • Learn about the componentless and empty-path routes
  • Take control of states in your application
  • Make use of imperative navigation
  • Understand guards and how they can benefit your applications
  • Optimize configuration and run tests on your routing

In Detail

Managing state transitions is one of the hardest parts of building applications. This is especially true on the web, where you also need to ensure that the state is reflected in the URL. In addition, you might want to split applications into multiple bundles and load them on demand. Doing this transparently isn’t easy. The Angular router solves these problems. Using the router, you can declaratively specify application states, manage state transitions while taking care of the URL, and load bundles on demand.

This book is a complete description of the Angular router written by its designer. It goes far beyond a how-to-get-started guide and talks about the library in depth. The mental model, design constraints, and the subtleties of the API-everything is covered. You’ll learn in detail how to use the router in your own applications. Predominantly, you’ll understand the inner workings of the router and how you can configure it to work with any edge cases you come across in your sites.

Throughout the book, you’ll see examples from real-world use in the MailApp application. You can view the full source of this application and see how the router code works to manage the state of the application and define what is visible on screen. Reading this book will give you deep insights into why the router works the way it does and will make you an Angular router expert.

Style and approach

This is an extremely practical book full of code examples and descriptions to help you understand the inner workings of the Angular router.

Downloading the example code for this book. You can download the example code files for all Packt books you have purchased from your account at If you purchased this book elsewhere, you can visit and register to have the code file.

Publisher resources

Download Example Code

Table of contents

  1. Angular Router
    1. Angular Router
    2. Credits
    3. About the Author
      1. - Angular consulting for enterprise customers, from core team members
      1. Why subscribe?
    5. Customer Feedback
    6. Preface
      1. What this book covers
      2. Who this book is for
      3. Conventions
      4. Reader feedback
      5. Customer support
        1. Downloading the example code
        2. Downloading the color images of this book
        3. Errata
        4. Piracy
        5. Questions
      6. Example
    7. 1. What Do Routers Do?
      1. Router configuration
      2. Router state
      3. Navigation
      4. Summary
      5. Isn't it all about the URL?
    8. 2. Overview
      1. URL format
      2. Applying redirects
      3. Recognizing states
      4. Running guards
      5. Resolving data
      6. Activating components
        1. Using parameters
      7. Navigation
        1. Imperative navigation
        2. RouterLink
      8. Summary
    9. 3. URLs
      1. Simple URL
      2. Params
      3. Query params
      4. Secondary segments
    10. 4. URL Matching
      1. Backtracking
      2. Depth-first
      3. Wildcards
      4. Empty-path routes
      5. Matching strategies
      6. Componentless routes
        1. Sibling components using same data
      7. Composing componentless and empty-path routes
      8. Summary
    11. 5. Redirects
      1. Local and absolute redirects
      2. One redirect at a time
      3. Using redirects to normalize URLs
      4. Using redirects to enable refactoring
    12. 6. Router State
      1. What is RouterStateSnapshot?
      2. Accessing snapshots
      3. ActivatedRoute
        1. URL
        2. Params
        3. Data
      4. Query params and fragment
    13. 7. Links and Navigation
      1. Imperative navigation
        1. Router.navigate
          1. Passing an array or a string
          2. Passing matrix params
          3. Updating secondary segments
          4. Relative navigation
          5. Forcing absolute navigation
          6. Navigation is URL-based
          7. Passing query params and fragment
        2. RouterLink
        3. Active links
          1. Exact matching
          2. Adding classes to ancestors
      2. Summary
    14. 8. Lazy Loading
      1. Example
        1. Just one problem
      2. Lazy loading
        1. Referring to lazily-loaded module
      3. Deep linking
      4. Sync link generation
      5. Navigation is URL-based
      6. Customizing module loader
      7. Preloading modules
        1. Enabling preloading
        2. Custom preloading strategy
    15. 9. Guards
      1. CanLoad
      2. CanActivate
      3. CanActivateChild
      4. CanDeactivate
    16. 10. Events
      1. Enable tracing
      2. Listening to events
      3. Grouping by navigation ID
      4. Showing spinner
    17. 11. Testing Router
      1. Isolated tests
      2. Shallow testing
      3. Integration testing
      4. Summary
    18. 12. Configuration
      1. Importing RouterModule
      2. Configuring router service
        1. Enable tracing
        2. Use hash
      3. Disable initial navigation
      4. Custom error handler
    19. A. Fin
      1. Bug reports
        1. Example app

Product information

  • Title: Angular Router
  • Author(s): Victor Savkin
  • Release date: March 2017
  • Publisher(s): Packt Publishing
  • ISBN: 9781787288904