Menu
CoddyTech

Path Parameters

Part of the Routing and State section of Coddy's Vue journey. Lesson 11 of 55.

A colon marks a variable path segment. The record below matches both /books/atlas and /books/poems. The matched segment is available as route.params.slug.

{ path: '/books/:slug', name: 'book', component: Book }
// In Book.vue setup:
const route = useRoute();
// Template: <h2>{{ route.params.slug }}</h2>

Use a named location with a params object when building a parameterized link, such as { name: 'book', params: { slug: 'atlas' } }. Parameter keys must match the record names. Ordinary single-segment params are strings, so a numeric-looking id is not automatically a number.

challenge icon

Challenge

Medium

Define a named exhibit route at /exhibits/:slug. In Exhibit.vue, obtain the current route and display Exhibit: followed by its slug in h2#view. Keep the supplied named Moon and Ocean links working for both parameter values.

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