Route-Selected Store Records
Part of the Routing and State section of Coddy's Vue journey. Lesson 42 of 55.
A route parameter can identify a record owned by a store. Derive the selected record from the current parameter, then pass that record identity to store actions. The URL selects the subject; the store owns its changing data.
const selected=computed(()=>
catalog.items.find(item=>item.id===route.params.id)
);
function toggle(){if(selected.value)catalog.toggle(selected.value.id)}The computed lookup follows reused route instances and updated store data. Guard missing records before displaying fields or offering record-specific actions. Passing a stable id is safer than assuming the visible record is always the first array entry.
Challenge
MediumConnect the detail route /clubs/:id to the shared club store. A computed lookup selects the current record and #toggle-detail calls the action with its current id. Show its name in h2#view and Saved or Not saved in p#status. Missing ids show only p#missing with Club not found. Updates must appear in Saved and remain isolated to the selected club.
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 Reservations3Dynamic Routes
Path ParametersReactive Route SelectionRoute Parameters as PropsMissing Route RecordsRecap: Equipment Routes6Route Policies
Redirecting Old PathsUnmatched PathsRoute MetadataCancelling NavigationRecap: Club Destinations9Shared Routed State
State Across ViewsRoute-Selected Store RecordsQuery and Store FiltersDraft and Commit OwnershipRecap: Reading Queue