Preserving Other Filters
Part of the Routing and State section of Coddy's Vue journey. Lesson 18 of 55.
A query update supplies the next query object. When one control changes a single key, copy the current query first so independent filters survive. Object spread followed by the changed key overwrites only that key.
router.push({query:{...route.query,order:'recent'}});
const {author,...remaining}=route.query;
router.push({query:remaining});Removing one key also needs a new object. Destructuring can separate the key being removed from the remaining fields. Avoid mutating route.query directly: ask the router to navigate so the location, history and rendered state stay consistent.
Challenge
MediumMake button#sort change only the query sort key, preserving other keys. It switches name to date and every other value to name. Display the current search or all in p#term, and sort or none in p#sort-value.
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