Dynamic Route Matching with Params
Very often we will need to map routes with the given pattern to the same component. For example, we may have a
User
component which should be rendered for all users but with different user IDs. In Vue Router we can use a dynamic segment in the path to achieve that, we call that a
param
:
Now URLs like
/users/johnny
and
/users/jolyne
will both map to the same route.
A
param
is denoted by a colon
:
. When a route is matched, the value of its
params
will be exposed as
route.params
in every component. Therefore, we can render the current user ID by updating
User
's template to this:
You can have multiple
params
in the same route, and they will map to corresponding fields on
route.params
. Examples:
pattern | matched path | route.params |
---|---|---|
/users/:username | /users/eduardo |
{ username: 'eduardo' }
|
/users/:username/posts/:postId | /users/eduardo/posts/123 |
{ username: 'eduardo', postId: '123' }
|
In addition to
route.params
, the
route
object also exposes other useful information such as
route.query
(if there is a query in the URL),
route.hash
, etc. You can check out the full details in the
API Reference
.
A working demo of this example can be found here .
Reacting to Params Changes
One thing to note when using routes with params is that when the user navigates from
/users/johnny
to
/users/jolyne
,
the same component instance will be reused
. Since both routes render the same component, this is more efficient than destroying the old instance and then creating a new one.
However, this also means that the lifecycle hooks of the component will not be called
.
To react to params changes in the same component, you can simply watch anything on the
route
object, in this scenario, the
route.params
:
Or, use the
beforeRouteUpdate
navigation guard
, which also allows you to cancel the navigation:
Catch all / 404 Not found Route
Regular params will only match characters in between url fragments, separated by
/
. If we want to match
anything
, we can use a custom
param
regexp by adding the regexp inside parentheses right after the
param
:
In this specific scenario, we are using a
custom regexp
between parentheses and marking the
pathMatch
param as
optionally repeatable
. This allows us to directly navigate to the route if we need to by splitting the
path
into an array: