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
MediumRead 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>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 Reservations