Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to do multiple nested route with Vue.js?

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.

like image 416
Jazuly Avatar asked Jan 01 '19 04:01

Jazuly


1 Answers

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,
              }]
          }]
      }]
   }]
})
like image 187
Billal Begueradj Avatar answered Nov 08 '22 07:11

Billal Begueradj