Menu
CoddyTech

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 icon

Challenge

Medium

Use 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>
quiz iconTest yourself

This lesson includes a short quiz. Start the lesson to answer it and track your progress.

All lessons in Routing and State