Search icon CANCEL
Subscription
0
Cart icon
Your Cart (0 item)
Close icon
You have no products in your basket yet
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Free Learning
Arrow right icon
Arrow up icon
GO TO TOP
Angular Router

You're reading from   Angular Router From Angular core team member and creator of the router

Arrow left icon
Product type Paperback
Published in Mar 2017
Publisher Packt
ISBN-13 9781787288904
Length 118 pages
Edition 1st Edition
Tools
Arrow right icon
Author (1):
Arrow left icon
Victor Savkin Victor Savkin
Author Profile Icon Victor Savkin
Victor Savkin
Arrow right icon
View More author details
Toc

Custom error handler

Every navigation will either succeed, will be cancelled, or will error. There are two ways to observe this.

The router.events observable will emit:

  • NavigationStart when navigation stars
  • NavigationEnd when navigation succeeds
  • NavigationCancel when navigation is canceled
  • NavigationError when navigation fails

All of them contain the id property we can use to group the events associated with a particular navigation.

If we call router.navigate or router.navigateByUrl directly, we will get a promise that:

  • will be resolved with true if the navigation succeeds
  • will be resolved with false if the navigation gets cancelled
  • will be rejected if the navigation fails

Navigation fails when the router cannot match the URL or an exception is thrown during the navigation. Usually this indicates a bug in the application, so failing is the right strategy, but not always. We can provide a custom error handler to recover from certain errors as shown in the following code:

function treatCertainErrorsAsCancelations...
lock icon The rest of the chapter is locked
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $19.99/month. Cancel anytime
Banner background image