Route Parameters as Props
Part of the Routing and State section of Coddy's Vue journey. Lesson 13 of 55.
Route props let a view receive data through its ordinary component interface. With props: true, the router passes the record's params to the matched component as props.
{path:'/labels/:code',component:LabelView,props:true}
// LabelView.vue:
defineProps({ code: String });The child can render the prop without importing the router, which makes it easier to reuse elsewhere. A props function can adapt route data, for example props: route => ({ code: route.params.code.toUpperCase() }). Keep that function a pure mapping; it can run again for later locations.
Challenge
MediumDecouple Exhibit.vue from the router. Declare a string code prop and show Badge: plus that prop in h2#view. Set props: true on the named badge route /badges/:code so its params become component props. Both supplied badge links must update the display.
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 Routes