🌿 Nuxt.js
Intermediate
How do dynamic routes work in Nuxt.js?
Answer
Dynamic routes in Nuxt.js are created by wrapping a filename segment in square brackets. For example, pages/posts/[id].vue matches /posts/1, /posts/hello, etc. Inside the component, you access the parameter via const route = useRoute(); route.params.id. You can have multiple dynamic segments: pages/[category]/[slug].vue. For catch-all routes, use [...slug].vue, which matches any number of path segments. Optional catch-all routes use [[...slug]].vue and also match the route with no trailing segments. For SSG, you must define which dynamic routes to pre-render using nitro.prerender.routes or the routeRules config.
Previous
What are Nuxt.js modules?
Next
What is the difference between `useAsyncData` and `useFetch`?