Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Multiple dynamic routes on same level impossible with Nuxt? #2982

Closed
kodekoder opened this issue Mar 9, 2018 · 15 comments
Closed

Multiple dynamic routes on same level impossible with Nuxt? #2982

kodekoder opened this issue Mar 9, 2018 · 15 comments
Labels

Comments

@kodekoder
Copy link

kodekoder commented Mar 9, 2018

I am trying to have such thing:

pages/
--| _section/
-----| index.vue
--| _slug/
-----| index.vue
--| index.vue

But looks like it doesn't work with Nuxt.

Is there any way to accomplish this?

This question is available on Nuxt.js community (#c2584)
@manniL
Copy link
Member

manniL commented Mar 9, 2018

You can do this definitely with a little workaround. Only use one dynamic route, e.g.:

pages/
--| _value/
-----| index.vue
--| index.vue

Then, use the /_value/index.vue for logic to differ between a section and a slug and render subsequent components based on this decision. ☺️

@kodekoder
Copy link
Author

Trying to figure out practical example, but have no ideas.

Right now I am receiving sections this way

axios.get(`articles/${params.section}`)

And slugs this way

axios.get(`articles/${params.section}/${params.slug}`)

Any ideas how should I refactor this? Maybe I can provide some Component related ID in my JSON? Fod example:

{
  "type": "sectionORslug",
  "data": ...
} 

I guess it could work?

@manniL
Copy link
Member

manniL commented Mar 10, 2018

Well, in your case you could nest your dynamic routes because slug would need both: section and slug. See how this (easily) works here

@kodekoder
Copy link
Author

Don't get it, how to apply to Nuxt routing?

@manniL
Copy link
Member

manniL commented Mar 10, 2018

Sorry, mislinked! You can see the theory in docs :)

In the end, a structure like this may help you:

pages/
--| _section/
-----| _slug/
--------| _slug.vue //For slugs
--------| index.vue //For sections
--| index.vue

@kodekoder
Copy link
Author

@manniL but then I will have route like this, no? /section/slug
If so, then this is not what I ask. I need /section and /slug on same level.

@kodekoder
Copy link
Author

I already have similar like this

screenshot from 2018-03-10 14-47-46

This is basically the same, right?

@manniL
Copy link
Member

manniL commented Mar 10, 2018

@jerakode Yeah, exactly. But how do you get the section parameter if you are in the _slug route?

Yup, the provided structure is similar to what I suggested ☺️

@kodekoder
Copy link
Author

Not sure what you ask. How do I get section in my current code? Given the route structure /section/slug I take it from params.section.

@manniL
Copy link
Member

manniL commented Mar 10, 2018

In your current code, this makes sense. But you want it on the same level. If so, how would you find out the section then when you are in the slug route?

@kodekoder
Copy link
Author

kodekoder commented Mar 10, 2018

I have all this data in JSON, I guess. All slugs (articles) are belongs to appropriate sections. Not sure how it can be solved right.

OK, what about next. We can have dynamic routes along with static on same level, right? I mean we can have dynamic section and static, lets say, special route (without underscore). So Nuxt can distinguish between this two. What if we have a fixed array of Sections? Can they behave then like static routes? I am anyway recieve a list of Sections on nuxtServerInit.

@manniL
Copy link
Member

manniL commented Mar 10, 2018

Well, then it might be useful to go for a /_section/_slug/ routing to map your data.

Exactly, you can use dynamic and static routes on the same level.
I'm not sure if it's possible to use a fixed route array as static routes programmatically. 🤷‍♂️

@supadrupa
Copy link

I have the same problem. How to fix it? I need multiple dynamic routes on same level. Could you help me please?

@merijnponzo
Copy link

merijnponzo commented Aug 6, 2018

similar problem

pages/_slug.vue
pages/index.vue
pages/recipes/_slug.vue
pages/recipes/index.vue

  • I generate a nav with router-links (this is a component in a layout)
  • they are rendered like /about, /team etc

Now I go to the recipes overview, with router-links like recipes/spaghetti
When I click on this item it matches
pages/recipes/_slug.vue

But my nav is suddenly replaced from /about, /team
to recipes/about, recipes/team

turned out I didnt add '/' before the segments in the main nav router-links
Now it works

@lock
Copy link

lock bot commented Nov 1, 2018

This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.

@lock lock bot locked as resolved and limited conversation to collaborators Nov 1, 2018
@danielroe danielroe added the 2.x label Jan 18, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Development

No branches or pull requests

5 participants