Reactive Route Selection
Part of the Routing and State section of Coddy's Vue journey. Lesson 12 of 55.
When two locations match the same dynamic record, Vue Router can reuse the component instance. A computed selection reads the current parameter each time it changes, so a product view follows navigation from one id to another.
const route = useRoute();
const products = {a: {name:'Notebook'}, b: {name:'Pencil'}};
const selected = computed(() => products[route.params.id]);A plain assignment such as const id = route.params.id captures only the value at setup. The same problem occurs if a ref is initialized from that value and never updated. Keep the derived read reactive; use a watcher only when a parameter change must trigger a side effect rather than derive display data.
Challenge
MediumUse a computed lookup in Exhibit.vue to select from the supplied records by route.params.id. Id 7 shows Night sky; id 8 shows Deep sea in h2#view. Switching between these parameter values reuses the same component. Preserve the supplied local tap counter rather than forcing a remount with a changing key.
Keep the supplied route readout and bootstrap files.
Try it yourself
<!doctype html><html><head><meta charset="UTF-8"><title>Routing practice</title></head><body><div id="app"></div><script type="module" src="./main.js"></script></body></html>This lesson includes a short quiz. Start the lesson to answer it and track your progress.
All lessons in Routing and State
4Query State
Reading Query ValuesCommitting Search QueriesPreserving Other FiltersNormalizing Query NumbersRecap: Catalog Filters2Navigation Actions
Navigation from HandlersReplacing HistoryHistory TravelCurrent Navigation LinksRecap: Save and Review5Nested Layouts
Parent and Child OutletsDefault Child ViewsRelative Child PathsAncestor Navigation StateRecap: Library Workspace8Store Design
Setup Store StructurePatching Related StateResetting Store StateComposing Related StoresRecap: Trip Pack11Workshop Planner Project
Planner RoutesShared Reservations3Dynamic Routes
Path ParametersReactive Route SelectionRoute Parameters as PropsMissing Route RecordsRecap: Equipment Routes