Optional Route parameters ?
can you not add optional route parameters?
something like the picture ?
as I want to handle the same route but sometimes with parameters and sometimes without
6 Replies
so i want this
/users
and
/users/xyz
to be routed through the same handler
Not through file based routing alone, no.
You'll need both
[slug].get.ts
to handle with params, and index.get.ts
to handle without.
You'll want to create a util that either acts as a wrapper (see https://nuxt.com/docs/guide/directory-structure/server#server-routes) for re-use or a method that executes shared logic and define a handler for both routes executing said util.am not sure how to do that from the given page
can i some how just redirect to another handler ? so i make the
index.get.ts
and [slug].get.ts
and make the index redirect to thatmaybe something like this ?
Exactly like that. You wouldn’t even need to import, auto imports!
In your shared handler, you can use
getRouterParam(event, 'slug')
which will be undefined in its absence thus you’ll treat it as the index. So on…just sharing that it is working great.
Ty for your support