Join GitHub today
GitHub is home to over 50 million developers working together to host and review code, manage projects, and build software together.
Sign upUnable to create a dynamic route with required parameter by following the guide #5874
Comments
|
Have a look at: For how it is supposed to work (specifically the route |
|
From what I can gather from this test is that one needs to create two routes, one for the intermediary path segment and other one for the parameter, like this:
But that would mean creating two routes. One that overrides the default case and rest will be handled by the parameter. Semantics wise this is ok as you'd normally expect the parent path tokens to always return something (however I don't exactly know if this is enforced by any sort of URI/HTTP specification). If we ignore the semantics, creating two routes works around the problem, whereas a cleaner solution would be just one route that contains the required parameter part. |
|
Is it really an intermediate path though? If you need _param3 to be required, vue-router still needs to know what to do when _param3 was not supplied. Therefore you need to create index.vue which forces you to implement a solution for when _param3 is empty. So required means here required for rendering the page component A. If the param was not supplied then you shouldnt use page component A but page component B because component A expect that param to eg do a API request. |
|
But why introduce the overhead of component B? I am quite sure that vue-router is capable of deciding that |
|
Found the culprit, it's a documentation change :( https://github.com/nuxt/docs/pull/888/files#diff-8017692b5218783a6438a7a1ed335c11 It looks like the two-file thing has been the intended pattern since the very beginning. I personally would still like a change in behavior as it feels cleaner to me, but I fully respect if this is voted against. |
|
@Ingramz what's the issue w/ the mentioned PR? |
|
@manniL sorry for being too vague. Essentially adding the word instead to the end of the sentence completely changed the instructions.
First you had
First you had |
|
Actually the first variant wouldn't work either because both files produce an equivalent result alone, the file needs to be |
|
Thanks for your contribution to Nuxt.js!
Issues that are labeled as |
|
Got it. @Ingramz would you mind to create a PR to fix it? |
|
I wouldn't mind, however I'll need to first understand whether my request of changing the behavior is reasonable compared to the way rest of the file to route mapping works. If it turns out that is not a feasible option, I guess we can fix the documentation instead based on the knowledge gained from understanding the behavior better. |
|
Just came across this issue after tripping on documentation too... :) Of course this kind of change would be potentially breaking so would require caution (maybe best suited for a major release?) - but that's definitely more in line with behaviour I'd expect. |


Version
v2.8.1
Reproduction link
n/a
Steps to reproduce
Create following file/directory structure:
What is expected ?
Routes to be generated as:
What is actually happening?
Routes are generated as:
Additional comments?
The guide says:
Following this advice (test2) results the parameter still being optional.
The test related to this functionality (
nuxt.js/packages/utils/test/route.test.js
Lines 174 to 184 in 2fabea9
I believe that any parameters specified as directories should be treated as required, so it would be possible to have full control over whether a route parameter is required or optional without resorting to any workarounds.