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

addRoute does not add new routes as children #600

Closed
antony-k1208 opened this issue Nov 20, 2020 · 8 comments
Closed

addRoute does not add new routes as children #600

antony-k1208 opened this issue Nov 20, 2020 · 8 comments
Labels
discussion This problem still needs more feedback

Comments

@antony-k1208
Copy link
Contributor

Version

4.0.0-rc.3

Reproduction link

https://codesandbox.io/s/bitter-dew-ie45g?file=/src/router/index.ts

Steps to reproduce

Check the link and open the console to see the output of all registered routes. Check the last object's children.

What is expected?

All dynamically added routes should be added to the Preview's route children array. Since I am adding them to that exact route.

What is actually happening?

Preview-First and Preview-Second are not getting added as child but as root routes with the path pointing to the correct location.

The children array of Preview does contain only the one added in the beginning via the routes array.

@posva
Copy link
Member

posva commented Nov 20, 2020

The children property of routes in getRoutes is the initial value, it's not the current children, it's the raw route record, it's not really meant to be used by the user, it's used internally when creating the routes. It could even be removed by the router to avoid confusion.

What would be the use case for having children? getRoutes already return all the routes

@posva posva added the discussion This problem still needs more feedback label Nov 20, 2020
@antony-k1208
Copy link
Contributor Author

Hey @posva, what a fast reply! Thanks for that.

I am trying to build a sub navigation for routes with children and the easiest why would be to iterate over that exact children array I guessed. But it breaks as soon as I am adding routes dynamically, which is too bad.

In some other project we wanted to be able to build a dynamic page builder and be able to persist the current user settings, for which we are using getRoutes among others. And here the same problem applies.

Any suggestions from your side? :)

Best,
Antony

@posva
Copy link
Member

posva commented Nov 20, 2020

What do you mean by sub navigation?

@antony-k1208
Copy link
Contributor Author

Like a multi level navigation? Like the one shown here for example: https://www.w3schools.com/howto/howto_css_subnav.asp

Can you check my Preview.vue from inside the views folder? Do you see how it is quite cumbersome to get all child routes for that particular route? I had to use a naming convention to be able to filter all routes somehow.

@posva
Copy link
Member

posva commented Nov 20, 2020

You realize that even if children contains the route records that are dynamically added, you will still need to filter the routes array for your use case since getRoutes returns all routes an a flattened array?

@antony-k1208
Copy link
Contributor Author

getRoutes returns them also as flattened or only? Because from what I can see in the in the console output. children is existent on all routes. So we could either update them when using addRoutes to have an exact representation or extend getRoutes by a parameter? When applied it only lists the route with its children as flattened array. All children should get a isChild property.

What do you think?

@posva
Copy link
Member

posva commented Nov 20, 2020

I think building a navbar shouldn't be part of the router code, it's application code that should format the routes exported by the router. getRoutes() should take no parameters, what is returned can be adapted but should go through an RFC like vuejs/rfcs#122.

I would rather see a parent property than an isChild property tbh. But besides that, this is unfortunately too vague to make sure the API covers all existing cases, an RFC would help gather feedback and expose what is necessary to users to create their own app-specific apis around it

For example, it's not clear if only original records should be pointed to inside children and parent, completely ignoring alias routes which are also present in the array returned by getRoutes

@antony-k1208
Copy link
Contributor Author

antony-k1208 commented Nov 20, 2020

@posva thank you very much for your time and feedback. Very much appreciated :). I will check your link and suggestions.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
discussion This problem still needs more feedback
Projects
None yet
Development

No branches or pull requests

2 participants