Back
VueEasyNew
How do you handle dynamic route parameters in Vue Router?
Colon-prefixed segments, useRoute()
Ideal Answer
Dynamic segments are declared with a colon prefix in the route path:
JavaScript
const routes = [
{ path: '/users/:id', component: UserDetail }
]
Inside the component, access the parameter via useRoute():
JavaScript
import { useRoute } from 'vue-router'
const route = useRoute()
const userId = route.params.id
By default, navigating between routes that only differ by param (e.g. /users/1 → /users/2) reuses the same component instance, so watch route.params if you need to react to param changes without a full remount.