Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Activate router-link that has multi level nested routes with CRUD setup

I trying to setup deep nesting like below, and I am kinda sure about we cannot use exact in router-link for nested routes.

<div id="app">
  <nav class="nav nav-main">
    <router-link exact to="/" class="nav-link" activeClass="active">Dashboard</router-link>
    <router-link to="/projects" class="nav-link" activeClass="active">Projects</router-link>
  </nav>

  <div class="parent-content">
    <h4>Content for Parent goes here</h4>
  </div>

  <router-view>
    <nav class="nav nav-main">
      <router-link :to="'/projects/' + $route.params.projectId" class="nav-link" activeClass="active">Deals</router-link>
      <router-link :to="'/projects/' + $route.params.projectId + '/commitments/'" class="nav-link" activeClass="active">Commitments</router-link>
    </nav>
    <router-view>
      <div class="child-content">
        <h4>Content for Child goes here</h4>
      </div>
    </router-view>
  </router-view>
</div>

My Route:

routes: [
  {
    path: '/',
    component: Dashboard
  },
  {
    path: '/projects',
    component: Projects
  },
  {
    path: '/projects/:id',
    name: 'projects-detail',
    component: ProjectDetails,
    children: [
      // DEALS
      {
        path: '/projects/:projectId/deals',
        component: Deals
      },
      {
        path: '/projects/:projectId/deals/:dealId/details',
        component: DealDetails
      },
      // COMMITMENTS
      {
        path: '/projects/:projectId/deals/:dealId/commitments/:commitmentId/edit',
        component: CommitmentEdit
      }
    ]
  }
]

With the above setup, I need to activate router-links, when the route is:
/projects/:projectId/deals/:dealId/details then activate Deals
/projects/:projectId/deals/:dealId/commitments/:commitmentId/edit then activate Commitments

like image 350
Syed Avatar asked Oct 28 '17 07:10

Syed


1 Answers

I think you have not another <router-view></router-view> inside ProjectDetails component add this and try.

Also remove /projects/:projectId from all child path as you have already in parent path path: '/projects/:id',

So final you route would be

routes: [
  {
    path: '/',
    component: Dashboard
  },
  {
    path: '/projects',
    component: Projects
  },
  {
    path: '/projects/:id',
    component: ProjectDetails,
    children : [
      // DEALS
      {
        path: 'deals/:dealId/details',//path will be /projects/:projectId/deals/:dealId/details
        component: DealDetails
      },
      {
        path: 'deals',.// path will be /projects/:projectId/deals
        component: Deals
      },
      // COMMITMENTS
      {
        path: '/deals/:dealId/commitments/:commitmentId/edit/',
        component: CommitmentEdit
      }
    ]
  }
]

Here is working fiddle : https://jsfiddle.net/chyLjpv0/16/

Read more about child path.

If you need not and component depended on parent dont make it as child use directly as root path like

routes: [
  {
    path: '/',
    component: Dashboard
  },
  {
    path: '/projects',
    component: Projects
  },
  {
    path: '/projects/:id',
    component: ProjectDetails,
   },
    // DEALS
    {
      path: '/projects/:projectId/deals/:dealId/details',
      component: DealDetails
    },
    {
      path: '/projects/:projectId/deals',
      component: Deals
    },
    // COMMITMENTS
    {
      path: '/projects/:projectId/deals/:dealId/commitments/:commitmentId/edit/',
      component: CommitmentEdit
    }
]

Working fiddle for this : https://jsfiddle.net/chyLjpv0/17/

Class router-link-exact-active is already working in this example : https://jsfiddle.net/chyLjpv0/18/ to display link as active

In your edit

Why you put <router-view> inside <router-view>. Outer is only working as it is being replaced and inner <router-view> is worthless. Use <router-view> in parent component for child component.

Also your inner <router-view> is not closed properly like </router-view>

like image 138
Niklesh Raut Avatar answered Oct 31 '22 04:10

Niklesh Raut