0

I'm using Vue and I currently have a page setup that is in the folder structure:

/pages/tg/_tg_id.vue

The setup is working fine as going to website.com/tg/<user-id> resolves to the user's page based off ID.

I'm now being asked to include a username in the URL, but I still need the id as names change and some user's don't even have a name. As a solution I'd like the new URL structure to be:

website.com/tg/<user-id>/<any string>

So that we can use a link that has the username in it, purely for vanity purposes (putting any string after the last slash should resolve in the exact same way).

I'm unsure of how to set something like this up, currently my nuxt.config.js file has no routing settings. Is there something I can add to settings to get the desired behavior?

1
  • Focos on the user id as the main parameter, and make the string that comes after optional. That's how most sites handle unique pages. If you need it to work without the name, in React you can create two different routes that render the same component. Makes two pages, and have each render the same component? Commented Feb 23, 2020 at 16:25

1 Answer 1

1

If you need only second option with username you can just rename your page to /pages/tg/_tg_id/_username.vue

and thats it.

If you still need your first url too you can use https://github.com/nuxt-community/router-extras-module and define alias inside your page or by extending routes by hand.

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks I'll mark as solved when I have a chance to test tomorrow. I do need both.
I actually found that just having the file you mentioned first was enough to handle both situations, thanks.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.