⚠️ This lesson is retired and might contain outdated information.

Catch All Undefined Paths with a Wildcard Route in Vue

Laurie Barth
InstructorLaurie Barth
Share this video with your friends

Social Share Links

Send Tweet

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.

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

[00:13] 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.

[00:33] 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.

[00:55] 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.

[01:16] 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.

[01:38] As we see, we can still hit Egghead without overwriting it.