10

is that posible to create Nested Routes more then 2?

i want to create something like this:

+--------------------+
| User               |
| +----------------+ |
| | Profile        | |
| | +------------+ | |
| | | About      | | |
| | | +--------+ | | |
| | | | Detail | | | |
| | | +--------+ | | |
| | +------------+ | |
| +----------------+ |
+--------------------+

so in the web will be like this

link: /localhost/user

web display:

USER

link: localhost/user/profile

web display:

USER
  PROFILE

link: localhost/user/profile/about

web display:

USER
  PROFILE
    ABOUT

link: localhost/user/profile/about/detail

web display:

USER
  PROFILE
    ABOUT
      DETAIL

any example code with jsfiddle will be very appreciated, thanks.

1 Answer 1

9

You just have to nest the corresponding routes (obviously you would also need the user's id parameter):

const router = new VueRouter({
  routes: [
    { path: '/user/:id', component: User,
      children: [
        {
          path: 'profile', component: Profile,
            children: [
              {
                path: 'about', component: About,
                  children: [
                    {
                      path: 'details', component: Details,
                    }
                  ]
              }
           ]
        }
      ]
    }
  ]
})

Same code, but just condensed (maybe it helps to read better):

const router = new VueRouter({
  routes: [{
    path: '/user/:id', component: User,
      children: [{
        path: 'profile', component: Profile,
          children: [{
            path: 'about', component: About,
              children: [{
                path: 'details', component: Details,
              }]
          }]
      }]
   }]
})
Sign up to request clarification or add additional context in comments.

2 Comments

Hi @billal-begueradj ! could you also explain how to switch routes in child pages with multiple pages in template using router-view
Could you improve the answer adding also the case where root paths or intermediate paths are not accessible because don't make sense? In this case a "fuzzy" component is mandatory? why a redirect is not enough?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.