How to use Nuxt SPA dynamic routes in development environment?

Viewed 41

I am new to Nuxt.js and working on an SPA project in which I need dynamic routes for a table.

Following the documentation, I was able to generate dynamic routes with: nuxt generate. But it only generates those routes for the build.

My question is, how can I work with dynamic routes in a development environment for testing and building the UI in my SPA project?

1 Answers

They are created on the fly while on dev mode, follow the instructions in the doc: https://nuxtjs.org/docs/features/file-system-routing#dynamic-routes

Create a given structure like

pages
 ┣ users/
 ┃ ┗ [id].vue

Then, go to something like /users/12 and you will have access to the dynamic page. Here is an example on how to proceed: https://stackoverflow.com/a/67490633/8816585

Don't forget to restart your dev server in case you created a new route (no need for generate btw, dev is enough).

PS: the syntax is _id.vue for Nuxt2 and [id].vue for Nuxt3 in case of a dynamic route.

Related