In the development process of Vue, there are sometimes problems of loading different routes according to the permissions of different people. After some tests, a solution is found, such as:

  1. Create a new config.json file in the router folder as the router's data source
[
  {
    "title": "Dashboard",
    "path": "/main/",
    "src": "dashboard",
    "summary": "statistics, charts, recent events and reports",
    "children": []
  },
  {
    "title": "Menu Example",
    "heading": true
  },
  {
    "title": "Multi Level Menu",
    "summary": "",
    "children": [
      {
        "title": "Item 1",
        "summary": "",
        "children": [
          {
            "title": "Item 11",
            "summary": "",
            "path": "/main/1",
            "children": []
          },
          {
            "title": "Item 12",
            "summary": "",
            "path": "/main/1",
            "children": []
          },
          {
            "title": "Item 13",
            "summary": "",
            "path": "/main/1",
            "children": []
          }
        ]
      },
      {
        "title": "Item 2",
        "summary": "",
        "path": "/main/1",
        "children": []
      },
      {
        "title": "Item 3",
        "summary": "",
        "path": "/main/1",
        "children": []
      }
    ]
  }
  1. Set router/index.js to be used for dynamically generating menus when exporting routerConfig

import Vue from 'vue'
import VueRouter from 'vue-router'
import routerConfig from './config.json'

Vue.use(VueRouter)

const routers = []

function recursionRouters (routerConfig) {
  for (let item of routerConfig) {
    if (item.src) {
      item.meta = Object.assign({}, item)
      item.component = resolve => require(['@/views/' + item.src], resolve)
      routers.push(item)
    } else if (item.children) {
      recursionRouters(item.children)
    }
  }
}

recursionRouters(routerConfig)

var router = new VueRouter({
  routes: [
    {path: '/', component: resolve => require(['@/views/index'], resolve)},
    {path: '/login', component: resolve => require(['@/views/index'], resolve)},
    {path: '/lock', component: resolve => require(['@/views/lock'], resolve)},
    {
      path: '/main',
      component: resolve => require(['@/views/main'], resolve),
      children: routers
    }
  ]
})

export default router

export {
  routerConfig,
  routers
}

  1. Write the component to generate the menu, recursively call itself to form infinite level menu
  Vue.component('page-sidebar-item', {
    template: `<li class="heading" v-if="model.heading">
                   <h3 class="uppercase">{{model.title}}</h3>
               </li>
               <li class="nav-item" v-else>
                  <router-link :to="model.path" class="nav-link" :class="{'nav-toggle':isFolder}" v-if="model.path">
                     <i class="icon-folder"></i>
                     <span class="title">{{model.title}}</span>
                     <span class="arrow" v-if="isFolder"></span>
                  </router-link>
                  <a href="javascript:;" class="nav-link" :class="{'nav-toggle':isFolder}" v-else>
                    <i class="icon-folder"></i>
                    <span class="title">{{model.title}}</span>
                    <span class="arrow" v-if="isFolder"></span>
                  </a>

                  <ul class="sub-menu" v-if="isFolder">
                    <page-sidebar-item v-for="model in model.children" :key="model.title" :model="model"></page-sidebar-item>
                  </ul>
               </li>`,
    props: {
      model: {type: Object}
    },
    computed: {
      isFolder: function () {
        return this.model.children &&
          this.model.children.length
      }
    }
  })
  1. Call component
<page-sidebar-item v-for="(sidebarItem, index) in sidebarData" :key="sidebarItem.title" :model="sidebarItem" :class="{'start active': index===0}"></page-sidebar-item>

All Comments

Leave a Reply Cancel Reply

Tips: Your email address will not be disclosed!

If you can't see clearly,please click to change...

Popular Posts

Collections