Working with nested routes can be quite a hassle...
Therefore I did come up with a conclusion on how to keep the routing configuration clean and...
For further actions, you may consider blocking this person and/or reporting abuse
Trying to understand how this is different than having all the "parent" and child routes at the top level in the first place. Seems like this has parent and child on the same level, right?
Iām not sure whether I understand you correctly.
You ask where the difference is between having these nested routes and the approach to not having nested routes and having all of them at the top level of the configuration?
If so, nested routes allow you to reuse code and logic. Imagine a detail view of a product in a store. You have a route to view the single product and edit it. With nested route you can reuse the code required for fetching the product or adding an access control.
In the original vue app, I presume that you have a file App.vue, which essentially serves as the EmptyRouterView for all of your other views, right? Without nesting routes, you could have separate routes like products/:id/details and products/:id/edit, etc. The solution described in the article seems to recreate this structure. If the parent is truly empty (my App.vue actually contains my navigation and notification components) and the others are at the same level, I don't see how you can reuse code any more than I reuse code from App.vue. What am I missing? For example, I am creating a structure that has Projects and Documents s.t. :doc belongs_to :project and :project has_many :docs and nesting sees to make logical sense there. I just want to do it the right way.
Okay first of all let me summarize the two approaches:
First Approach: No Nested Routes
Main
App.vue
includes the application layout with nav, footer etc.The router config has the routes on the top - level
The pages themselves then mainly just contain the content of the page
Benefits: Application layout is reused, simple
Shortcomings: nearly no code-reuse regarding routing is possible (e.g. navigation guards, path definitions)
Second Approach: Nested Routes
The
App.vue
file is just the root router viewThe router config is nested
The pages then contain the whole application code:
Benefits: code reuse in navigation, route structure resembles logical structure of entities
Shortcomings: Pages include duplicate code regarding application structure
My preferred approach: Nested routes with layouts
Now we can use the concept of layouts, to enable the pages to reuse application structure. This way we can diminish the shortcomings of the bare nested router approach. But in general the layouts are independent from the routing approach and just a good practice imo.
Layouts are simple components that hold the application structure (nav, footer, side-menu) for different pages.
Then the pages can use these layouts like so:
That way the pages do not repeat the basic code for application structure. Also you can easily have different layouts for different pages. For example, some pages maybe should have a sidebar, while others shouldn't. This can easily be abstracted from the pages.
If you want to know more about the concept of layouts, I can also write a more detailed post on this. :)
Ok this is helpful, thanks. I hadn't thought of the beforeEnter callbacks. I had been using mounted/created for that in each component, but if I understand it correctly, I like the abstraction. Now I am wondering why we can't just keep reusing the layout stuff in App.vue and do the nesting with your empty parent solution described in your article. I haven't used slots much so that is something I need to learn. So far props have sufficed for me.
I would keep the
App.vue
and theEmptyRouterViewComponent
just empty with the<router-view/>
. This way the pages can control the whole appearance (through the layouts). I would let the pages do this (and not theApp.vue
). Imagine you would hard-code the nav and a side menu into theApp.vue
and now you want a Login page not to have a side menu. This may be pretty difficult to override that way. When the pages control the layout, this is way more dynamic and adaptable.You can also take a look at this project, which uses the approach I outline in the comment above, to understand how this would work on a more complex application than the example: github.com/BerniWittmann/cape-fron...
In general slots are a very handy tool, so i would recommand taking a more in-depth look (e.g. named slots). Especially components can profit from slots. For example, a card component that renders some content is more versatile with a slot instead of props for the content.
Ok. I'm converted. I am going to check out slots.
Thanks again.
Thanks, this was super helpful. I was setting up an onboarding flow with the top level route being the initial account creation form, which then led to a profile form, etc. and trying to get the initial form to go away when the child routes were hit was driving me nuts. The absract EmptyRouterView worked like a charm.
The idea is pretty damn cool. Unfortunately my menu is rendered by
$router.options.routes
so when it encounterEmptyRouterView
(which has no name and this is necessary to set a link) the menu stops working. Maybe I missed something...Yes that's right for your special use case. But you could utilize
meta
data in the route configuration, to control whether a route should be visible in the menu, and also have stuff like navigation names etc. there.The comment above yours with the breadcrumbs has kind of the same problematic, which can also be solved be
meta
data. And it makes no difference whether you use a breadcrumb navigation or your app's menu :) Hope that helps ;)I found I had to add a bit more, because you can't have name github.com/vuejs/vue-router/issues...
Things like breadcrumbs won't work hence I adding something like meta.label to your route enabled me to filter and add breadcrumbs
Naming the label the same as the default child allow a breadcrumb in the place of the empty component
Good point. I think it's a good idea to use
meta.label
for a Breadcrumb navigation anyway, since the translation keys mostly have a different convention than route names and therefore probably are different. šš»I have used a similar setup, but faced the following problem with it. I have a bunch of routes that all use an EmptyRouterView as base view. This way I wanted to put some reusable logic*, including a dialog box, in the EmptyRouterView (I guess not so empty anymore...). For a while I was fooled into thinking this worked, until I navigated between routes in my SPA, and noticed that the setup() (of the Vue Composition API) was not called anymore!
{
path: '/my-route',
component: EmptyRouterView,
children: [{
path: '',
component: MyRouteView
}]
},
{
path: '/my-other-route',
component: EmptyRouterView,
children: [{
path: '',
component: SomeOtherView
}]
}
It seems that JS/Webpack is too smart and thinks that since it's the same module it does not need any setup() anymore. A real bummer. Idea's?
*) The resusable logic is stuff that's shared by a lot of pages and includes a beforeRouteUpdate (which insists on being placed in the VIEW) and a dialog box that asks if the user wants to discard changes made. Both difficult things to make reusable, I found..
Perfect. Just the problem I was having solved.