Route params and query
A list page links to many detail pages, and each needs its own URL. A dynamic
segment captures the varying part: a route path of /users/:id matches
/users/42 and /users/7, with id holding the value.
const router = createRouter({
history: createWebHistory(),
routes: [{ path: '/users/:id', component: UserDetail }],
});
Inside the component, the useRoute() composable returns the current route.
Read the captured value from route.params:
<script setup>
import { useRoute } from 'vue-router';
const route = useRoute();
</script>
<template>
<h1>User {{ route.params.id }}</h1>
</template>
The part after ? is the query, and it never appears in the path pattern.
/search?q=vue&page=2 gives you route.query.q and route.query.page, both
strings. Use params for the resource's identity and query for optional state
like a page number or a filter.
When you build a link, pass params as an object instead of gluing strings:
<RouterLink :to="{ name: 'user', params: { id: user.id } }">Profile</RouterLink>
Naming the route (name: 'user') lets the router build the URL for you, so the
link keeps working if the path pattern changes.
You are about to answer a short quiz on dynamic params and query.