Join egghead, unlock knowledge.

Want more egghead?

This lesson is for members. Join us? Get access to all 3,000+ tutorials + a community with expert developers around the world.

Unlock This Lesson
1×
Become a member
to unlock all features

Level Up!

Access all courses & lessons on egghead today and lock-in your price for life.

Autoplay

    Catch All Undefined Paths with a Wildcard Route in Vue

    Laurie BarthLaurie Barth
    vueVue.js
    >=2.5.22

    We can use the "*" symbol to catch all matching paths. However, there are ways to do this that will override other paths. We want to avoid those.

    Code

    Code

    Become a Member to view code

    You must be a Member to view code

    Access all courses and lessons, track your progress, gain confidence and expertise.

    Become a Member
    and unlock code for this lesson
    Discuss

    Discuss

    Transcript

    Transcript

    Instructor: We'll start by importing a new component called PageNotFound. This will act as a stand-in for our 404 page or similar.

    We're going to create a new route record. We'll set the component to PageNotFound, but the path will be /*. Star is a wildcard value that will match everything it hits.

    If we navigate to test, which is a URL we haven't defined, we'll end up on PageNotFound. If we move this route record up in our array, ahead of the Egghead path, and we try and navigate to test, we'll see what we want to see.

    If we navigate to Egghead, we find that it's overwritten because / is hitting the path. What we want to do is remove the / and keep just , which will allow us to hit other URLs that are also matching but still act as that wildcard catch-all.

    Alternatively, we can set our path to PageNotFound and alias that path to *. This allows us to do what we did before but also access PageNotFound directly through a URL if we were to want to navigate to it from another component.

    As we see, we can still hit Egghead without overwriting it.