Question presented to candidate:
"We have a user profile page at /users/:id. Explain how dynamic segments and params work in Vue Router, how the page gets the id, and what goes wrong when the user clicks from one profile straight to another."
What a strong answer should cover:
- A
:namesegment in a routepathmatches one URL segment and exposes it asroute.params.name, always a string (or an array for repeatable params). - Reading params:
useRoute().params, or better,props: trueon the route so the component takes the param as a prop and stays router-agnostic. - Advanced patterns: custom regex (
:id(\d+)), optional (?), repeatable (+/*), and the catch-all/:pathMatch(.*)*. - Component reuse: going from
/users/1to/users/2keeps the same instance, soonMounteddoes not run again; react withwatchon the param oronBeforeRouteUpdate, or force a remount with:key. - Building links with named routes and
params(encoded for you) instead of string concatenation; a missing required param throws.
Clarifying questions expected:
- "Should a profile-to-profile switch keep scroll position and local state, or start fresh?"
- "Are ids numeric only, so we could reject
/users/abcat the router level?"
Code / implementation expected: Yes β a route with :id and props: true, and a component that reloads data when the param changes.