Menu
CoddyTech

Reading Query Values

Part of the Routing and State section of Coddy's Vue journey. Lesson 16 of 55.

Query keys describe optional view state after the path. For /library?category=art, the path still selects the library and route.query.category supplies an optional filter. Query values can be strings, null for a bare key, or arrays for repeated keys.

const route=useRoute();
const category=computed(() =>
  typeof route.query.category === 'string' ? route.query.category : 'all'
);

Choose a deliberate fallback instead of assuming every key exists or always contains one string. Reading the query inside a computed getter keeps the filter connected when navigation changes only the query. A query-only change does not need a new route record.

challenge icon

Challenge

Medium

Read query key q reactively in the root view. Show Search: [value] in p#term. A missing key means an empty string, so the All link shows Search: []. Follow both supplied nonempty queries without recreating the app.

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