question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Empty parent in nested routes

See original GitHub issue

It could be interesting to have a parent route definition without any component and having the matched children rendered in the root <router-view>. Here is an example:

var routes = [
  {
    path: '/:lang',
    children: [
      {
        path: '',
        component: WelcomeComponent
      },
      {
        path: 'hello/:name',
        component: HelloComponent
      }
    ]
  }
]

This won’t work. Instead, this is what I am doing:

var routes = [
  {
    path: '/:lang',
    // create a container component
    component: {
      render (c) { return c('router-view') }
    },
    children: [
      {
        path: '',
        component: WelcomeComponent
      },
      {
        path: 'hello/:name',
        component: HelloComponent
      }
    ]
  }
]

But the original problem was to scope my routes by their definitions only, not by components.

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Reactions:59
  • Comments:23 (3 by maintainers)

github_iconTop GitHub Comments

35reactions
dschreijcommented, Aug 31, 2017

My take on this:

const withPrefix = (prefix, routes) => 
    routes.map( (route) => {
        route.path = prefix + route.path;
        return route;
    });

export const routes = [
    { 
        path: '/',
        component: Home 
    },
    { 
        path: '/contacts',
        component: Contacts,
        props:{
            pageTitle: 'Contacts'
        }
    },
    ...withPrefix('/settings',[
        { 
            path: '/account',
            component: AccountSettings,
            props:{
                pageTitle: 'Account settings'
            },
        },
        { 
            path: '/application',
            component: AppSettings,
            props:{
                pageTitle: 'Application settings'
            },
        },
    ]),
]

Pretty straightforward, although I can understand the desire for a utility function for this.

22reactions
ghostcommented, Aug 30, 2017

kinda shitty that you can’t just omit the Component when you want the child to be rendered instead of the parent…

Read more comments on GitHub >

github_iconTop Results From Across the Web

React-Router nested routes loading blank page instead of ...
You need to remove the exact prop from the Dashboard route (present in Switch ) while doing the child routing.
Read more >
[Vue] Empty parent in Nested Routes of Vue Router
const router = new VueRouter({ routes: [{ path: '/', component: Home, children: [{ path: 'users', component: UserPage, children: [{
Read more >
react router v6 how to leave parent element
The Guide to Nested Routes with React Router ... You declaratively nest the child Route as a children of the parent Route. Like...
Read more >
Nested Routes
With Vue Router, you can express this relationship using nested route configurations. Note that nested paths that start with / will be treated...
Read more >
Routing
Nested Routes - The general idea of routes mapping to segments of the URL ... Parent Layout Route or Parent Route - A...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found