el-menu動態加載路由的實現

先看需要實現的效果

這裡有一級也有二級菜單,註意二級菜單的父目錄(”選項設置“點擊不會跳轉,隻是展開目錄),然後點擊去詳情頁,需要跳到一個隱藏的路由,不在菜單展示的路由

還有一點要註意,就是這裡有兩個router-view,整個頁面是一個router-view,可以由LoginView和HomeView替換(當前看到的頁面),而HomeView下又有一個router-view,需要用來展示部門,系統,超時,員工設置,不合格品列表和不合格品詳情頁。

以上的信息均需要在數據庫的表中體現

先看看直接寫在代碼裡需要哪些操作

const routes = [
  {
    path: '',
    name: 'login',
    component: LoginView,
  }
  ,
  {
    component: HomeView,
    children: [
      {
        path: '/home',
        name: '不合格品列表',
        component: BelowStandard
      },
      {
        path: '/product/:id',
        name: '不合格品詳情',
        component: BelowStandardDetail
      }
    ]
  },
  {
    component: HomeView,
    name: '選項設置',
    children: [
      {
        path: '/employee',
        name: '員工設置',
        component: EmployeeConfig,
      },
      {
        path: '/department',
        name: '部門設置',
        component: DepartmentConfig
      },
      {
        path: '/system',
        name: '系統設置',
        component: SystemConfig
      },
      {
        path: '/warn',
        name: '超時提醒',
        component: WarmConfig
      }
    ]
  },
  {
    component: HomeView,
    children: [
      {
        path: '/statistics',
        name: '統計',
        component: DailyStatistics
      }
    ]
  },
  {
    component: HomeView,
    children: [
      {
        path: '/log',
        name: '日志管理',
        component: LogManager
      }
    ]
  },
]

這是路由,當要動態從數據庫加載時,就不能寫在這

<el-menu
            router
            active-text-color="#ffd04b"
            background-color="#000"
            class="el-menu-vertical-demo"
            :default-active="this.$route.path"
            text-color="#fff"
            @open=""
            @close=""
        >
          <el-menu-item index="/home">
            <template #title>
              不合格品列表
            </template>
          </el-menu-item>
          <el-sub-menu index="/subMenuConfig">
            <template #title>
              選項設置
            </template>
            <el-menu-item index="/department">部門設置</el-menu-item>
            <el-menu-item index="/system">系統設置</el-menu-item>
            <el-menu-item index="/warn">超時設置</el-menu-item>
            <el-menu-item index="/employee">員工設置</el-menu-item>
          </el-sub-menu>
          <el-menu-item index="/statistics">
            <span>統計</span>
          </el-menu-item>
          <el-menu-item index="/log">
            <span>日志管理</span>
          </el-menu-item>
        </el-menu>

這是el-menu開啟瞭路由功能,所以能跳轉路由,當動態加載的時候,這部分需要改造成v-for

數據庫

說明:parent_id為0的即是一級目錄,但是一級目錄裡一部分可以直接展示界面,一部分是展開二級目錄,我這是以component字段為home/HomeView.vue來區分是展示二級目錄。

現在開始寫後端程序,返回菜單的json格式數據。

List<Menu> menuList = menuMapper.getMenuByUserId(UserUtils.getLoginUser().getId());
//根據ParentId分組
Map<Integer, List<Menu>> map = menuList.stream().collect(Collectors.groupingBy(Menu::getParentId, TreeMap::new,Collectors.toList()));
List<Menu> menus = map.get(0);//一級菜單
menus.forEach(menu->{//給有二級菜單的目錄設置children屬性
    List<Menu> children = map.get(menu.getId());
    menu.setChildren(children);
});
return menus;

從數據庫查詢到的數據格式如圖,然後分一級二級菜單處理後,再返回前端

[
{
"name": "不合格品列表",
"path": "/home",
"component": "product/BelowStandard.vue",
"orderNum": 1,
"parentId": 0,
"isHidden": false,
"children": null
},
{
"name": "選項設置",
"path": "/subMenuConfig",
"component": "home/HomeView.vue",
"orderNum": 2,
"parentId": 0,
"isHidden": false,
"children": [
				{
				"name": "員工設置",
				"path": "/employee",
				"component": "config/EmployeeConfig.vue",
				"orderNum": 1,
				"parentId": 2,
				"isHidden": false,
				"children": null
				},
				{
				"name": "部門設置",
				"path": "/department",
				"component": "config/DepartmentConfig.vue",
				"orderNum": 2,
				"parentId": 2,
				"isHidden": false,
				"children": null
				},
				{
				"name": "系統設置",
				"path": "/system",
				"component": "config/SystemConfig.vue",
				"orderNum": 3,
				"parentId": 2,
				"isHidden": false,
				"children": null
				},
				{
				"name": "超時提醒",
				"path": "/warn",
				"component": "config/WarmConfig.vue",
				"orderNum": 4,
				"parentId": 2,
				"isHidden": false,
				"children": null
				}
]
},
{
"name": "統計",
"path": "/statistics",
"component": "statistics/DailyStatistics.vue",
"orderNum": 3,
"parentId": 0,
"isHidden": false,
"children": null
},
{
"name": "日志管理",
"path": "/log",
"component": "log/LogManager.vue",
"orderNum": 4,
"parentId": 0,
"isHidden": false,
"children": null
},
{
"name": "不合格品詳情",
"path": "/product/:id",
"component": "product/BelowStandardDetail.vue",
"orderNum": 5,
"parentId": 0,
"isHidden": true,
"children": null
}
]

前端得到數據之後進行處理,再添加到路由,過程中遇到一個問題,vue-router4版本去掉addRoutes換成addRoute帶來的問題困擾我很久

用Vue3就必須用Router4.x版本,由於4.0去掉瞭addRoutes 所以隻能用addRoute

現在是隻能添加一個

function routerPackag(routers:any) {
  if (routers) {
    routers.filter((itemRouter:any) => {
      if (itemRouter.component != "Layout") {
        router.addRoute('home',{ //home是父組件 add-route添加進父組件chilren裡
          path: `${itemRouter.path}`,
          name: itemRouter.name,
          meta: {
            title: itemRouter.name,
          },
          component: () => import(`../views/${itemRouter.component}`),
        })
      }
      if (itemRouter.children && itemRouter.children.length) {
        routerPackag(itemRouter.children)
      }
      return true
    })
  }
}

初始化路由:

router.beforeEach((to, from, next) => {//配置路由守衛
    if(to.path==='/'){
        next()
    }else if(store.state.user.id){
        initMenus(router,store,next,to)
    }else{
        next({ path: '/',query: {redirect: to.path}});
    }
});

export const initMenus = (router, store,next,to) => {//按F5刷新的話vuex裡的會被清空,長度變為0
    if (store.state.menu !== null) {
        next()
    }else {
        axios.get("/menu").then(response => {
            if (response) {
                let responseData = response.data
                if (responseData.flag) {
                    store.state.menu = responseData.data
                    initRoute(router,store.state)
                    next({...to,replace:true})//解決router4版本的第一次路由不匹配問題
                } else {
                    this.$ElMessage.error('請求菜單失敗')
                }
            }
        })
    }
}

const initRoute = (router,state)=> {
    const loadView = view => {//這種引入方式控制臺不會報警告
        // 路由懶加載
        return () => import(`@/views/${view}`)
    };
    const menus = state.menu
    const firstLevelMenu = {
        children: [],
        component: loadView('home/HomeView.vue')
    }
    menus.forEach(menu=>{
        menu.component = loadView(menu.component)
        if(menu.children === null || menu.children.length === 0){
            firstLevelMenu.children.push(menu)
        }else{
            menu.children.forEach(children=>{
                children.component = loadView(children.component)
            })
            router.addRoute(menu)
        }
    })
    router.addRoute(firstLevelMenu)
}

完成這些配置之後,路由就能動態加載瞭,然後取出vuex中存儲的menu生成el-menu

vuex中菜單大致如圖

<el-menu
    router
    active-text-color="#ffd04b"
    background-color="#000"
    class="el-menu-vertical-demo"
    :default-active="this.$route.path"
    text-color="#fff"
    @open=""
    @close=""
>
  <template v-for="route of this.$store.state.menu">
    <template v-if="route.children === null || route.children.length === 0"><!--一級菜單-->
      <template v-if="!route.isHidden">
        <el-menu-item :index = "route.path">
          <span>{{route.name}}</span>
        </el-menu-item>
      </template>
    </template>
    <template v-else><!--二級菜單-->
      <template v-if="!route.isHidden">
        <el-sub-menu :index = "route.path">
          <template #title>
            <span>{{route.name}}</span>
          </template>
          <template v-for="children of route.children">
            <template v-if="!children.isHidden">
              <el-menu-item :index = "children.path">
                <span>{{children.name}}</span>
              </el-menu-item>
            </template>
          </template>
        </el-sub-menu>
      </template>
    </template>
  </template>
</el-menu>

實現效果展示

到此這篇關於el-menu動態加載路由的實現的文章就介紹到這瞭,更多相關el-menu動態加載路由內容請搜索WalkonNet以前的文章或繼續瀏覽下面的相關文章希望大傢以後多多支持WalkonNet!

推薦閱讀: