Vue3 Fundamentals ยท Vue Router

Route params and query

Dynamic params and query

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.

  1. In the path `/users/:id`, the `:id` part is a ___ segment.
  2. `useRoute()` returns the current route, including its `params` and `query`.
  3. In `/search?q=vue`, the value `vue` is read from `route.___`.
  4. Values read from `route.query` are always strings.
  5. A query string is part of the route's path pattern.
  6. Passing `{ name: 'user', params: { id } }` to `<RouterLink>` lets the router build the ___ for you.