javascriptvue.jsvue-router

What is the best way to generate menu titles with vue router?


I have the following routes defined in vue router:

const routes = [
  {
    path: '/',
    name: 'home',
    params: { label: 'Home' },
    component: Home
  },
  {
    path: '/path1',
    name: 'path1',
    params: { label: 'Path 1' },
    children: [
      {
        path: 'subpath1',
        name: 'path1-sub1',
        params: { label: 'Sub 1' },
        component: sub1,
      },
      {
        path: 'subpath2',
        name: 'path1-sub2',
        params: { label: 'Sub 2' },
        component: sub2,
      },
      {
        path: 'subpath3',
        name: 'path1-sub3',
        params: { label: 'Sub 3' },
        component: sub3,
      }
    ]
  },
  {
    path: '/path2',
    name: 'path2',
    params: { label: 'Path 2' },
    children: [
      {
        path: 'subpath2',
        name: 'path2-sub1',
        params: { label: 'Sub 1' },
        component: sub1,
      },
      {
        path: 'subpath2',
        name: 'path2-sub2',
        params: { label: 'Sub 2' },
        component: sub2,
      },
      {
        path: 'subpath3',
        name: 'path2-sub3',
        params: { label: 'Sub 3' },
        component: sub3,
      }
    ]
  },
  {
    path: '/path3',
    name: 'path3',
    params: { label: 'Path 3' },
    children: [
      {
        path: 'subpath3',
        name: 'path3-sub1',
        params: { label: 'Sub 1' },
        component: sub1,
      },
      {
        path: 'subpath2',
        name: 'path3-sub2',
        params: { label: 'Sub 2' },
        component: sub2,
      },
      {
        path: 'subpath3',
        name: 'path3-sub3',
        params: { label: 'Sub 3' },
        component: sub3,
      }
    ]
  },
]

I'm trying to make a v-for loop that generates a navigation menu for my app, but I'd like to add a title between path2 and path3 and have some way to quickly add titles in the future. Is there any way I can add a dummy item as a route or is there something vue router uses already to generate titles in menus?


Solution

  • I am not supre sure if I understood you correctly. But form what I gather I would probably do something like this:

    // I added an title "object"
    const routes = [
      {
        path: '/',
        name: 'home',
        params: { label: 'Home' },
        component: Home
      },
      {
        path: '/path1',
        name: 'path1',
        params: { label: 'Path 1' },
        children: [
          {
            path: 'subpath1',
            name: 'path1-sub1',
            params: { label: 'Sub 1' },
            component: sub1,
          },
          {
            path: 'subpath2',
            name: 'path1-sub2',
            params: { label: 'Sub 2' },
            component: sub2,
          },
          {
            path: 'subpath3',
            name: 'path1-sub3',
            params: { label: 'Sub 3' },
            component: sub3,
          }
        ]
      },
      {
        path: '/path2',
        name: 'path2',
        params: { label: 'Path 2' },
        children: [
          {
            path: 'subpath2',
            name: 'path2-sub1',
            params: { label: 'Sub 1' },
            component: sub1,
          },
          {
            path: 'subpath2',
            name: 'path2-sub2',
            params: { label: 'Sub 2' },
            component: sub2,
          },
          {
            path: 'subpath3',
            name: 'path2-sub3',
            params: { label: 'Sub 3' },
            component: sub3,
          }
        ]
      },
      {
         title: "I am some title",
         as: "h4"
      },
      {
        path: '/path3',
        name: 'path3',
        params: { label: 'Path 3' },
        children: [
          {
            path: 'subpath3',
            name: 'path3-sub1',
            params: { label: 'Sub 1' },
            component: sub1,
          },
          {
            path: 'subpath2',
            name: 'path3-sub2',
            params: { label: 'Sub 2' },
            component: sub2,
          },
          {
            path: 'subpath3',
            name: 'path3-sub3',
            params: { label: 'Sub 3' },
            component: sub3,
          }
        ]
      },
    ];
    

    and then use the array as is for you UI and for vue-router you do:

    
    const getRoutes = () => routes.filter(route => !route.title)
    
    

    If this is not what you were looking for please clarify.