Menu
CoddyTech

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 icon

Challenge

Medium

Make 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>
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