Normalizing Query Numbers
Part of the Routing and State section of Coddy's Vue journey. Lesson 19 of 55.
A numeric-looking query is still external text. Convert it and then validate the domain before arithmetic. A page number generally needs an integer lower bound, not merely a value that can be parsed.
const page=computed(()=>{
const value=typeof route.query.sheet==='string'?Number(route.query.sheet):NaN;
return Number.isInteger(value)&&value>=1?value:1;
});A fallback can control the display without rewriting the requested location. Keeping normalization in one computed value gives buttons and labels the same interpretation of invalid input. Decide explicitly how missing values, repeated keys, fractions and zero should behave.
Challenge
MediumNormalize the query page in a computed value. Accept a string that converts to an integer at least 1; missing, repeated, nonnumeric, fractional and nonpositive values use 1. Show Page n in p#page and Offset x in p#offset, where x is (n - 1) * 5. Do not rewrite invalid locations.
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