1. 3
    Catch an invalid route navigation with the Angular router
    1m 10s
⚠️ This lesson is retired and might contain outdated information.

Catch an invalid route navigation with the Angular router

Share this video with your friends

Social Share Links

Send Tweet
Published 7 years ago
Updated 6 months ago

Usually on web sites, if you pay attention to a good user experience, you define a custom 404 page, telling the user he arrived at some dead end. Similarly we should do for our Angular application routing. In this lesson we learn how to define such “404 route” using the Angular router.

In the sample application, we have defined different kind of routes which we can navigate to by changing the URL up here. Now, what happens if the user types in some URL which doesn't exist? Our application redirects simply to the home, and nothing happens.

We want obviously to have a better user experience. Let's generate a new component. Let's call it notFound. Then we go to our app module, and at the very end here, we define a route that is going to catch all those routes which aren't matched.

We do that by defining here to asterisk. As a component, as you can imagine, we specify our notFound component. Let's open the notFound component, specify here, "Sorry, nothing to see here." Great. Let's save this.

Now, whenever we go to our routing component here, our normal routes just work. Where we a specify a route which doesn't exist, our component here, notFound, gets loaded and displays something nice to the user.

egghead
egghead
~ a minute ago

Member comments are a way for members to communicate, interact, and ask questions about a lesson.

The instructor or someone from the community might respond to your question Here are a few basic guidelines to commenting on egghead.io

Be on-Topic

Comments are for discussing a lesson. If you're having a general issue with the website functionality, please contact us at support@egghead.io.

Avoid meta-discussion

  • This was great!
  • This was horrible!
  • I didn't like this because it didn't match my skill level.
  • +1 It will likely be deleted as spam.

Code Problems?

Should be accompanied by code! Codesandbox or Stackblitz provide a way to share code and discuss it in context

Details and Context

Vague question? Vague answer. Any details and context you can provide will lure more interesting answers!

Markdown supported.
Become a member to join the discussionEnroll Today