码迷,mamicode.com
首页 > 其他好文 > 详细

6-项目左侧菜单栏显示

时间:2020-02-23 20:29:35      阅读:67      评论:0      收藏:0      [点我收藏+]

标签:for   from   element   where   遇到的问题   首页   ali   mit   进一步   

1、后台处理

技术图片

 

 

 查询主菜单和子菜单很简单,首先菜单类中要有自己的子类集合

技术图片

 

 

 然后,将菜单进行笛卡儿积查询两次,将查询后的结果进行如下筛选

m1.id=m2.parentId

m1.id!=1

m2.id in(这个根据一个个查询,最后查到该mid)

根据m1.id,m2.id 进行排序

select m1.`id`,m1.`path`,m1.`component`,m1.`iconCls`,m1.`name`,m1.`requireAuth`,m2.`component` as component2,m2.`iconCls` as iconCls2,
m2.`keepAlive` as keepAlive2,m2.`name` as name2,m2.`path` as path2,m2.`requireAuth` as requireAuth2 from menu m1,menu m2 --笛卡尔积
where m1.`id`=m2.`parentId`--查询父子关系
and m1.`id`!=1 --去掉根节点
and m2.`id` in(select mr.`mid` from hr_role h_r,menu_role mr where h_r.`rid`=mr.`rid` and h_r.`hrid`=#{hrId})--查询那个用户才具有该权限
and m2.`enabled`=true order by m1.`id`,m2.`id` --进一步筛选、排序

2、前端处理

前端的处理稍微麻烦,为了想把后台传递来的json转化为Vue所需要的形状,所以要构建一个工具类来专门处理该过程

store中要定义一个数据来保存菜单数据

技术图片

 

 书写转换方法,转换后将数据保存到上面routes中,供后面调用

import {getRequest} from ‘./api‘
import {Message} from ‘element-ui‘
export const initMenu = (router, store)=> {
  if (store.state.routes.length > 0) {
    return;
  }

  getRequest("/config/sysmenu").then(resp=> {
      
    if (resp && resp.status == 200) {
      var fmtRoutes = formatRoutes(resp.data);
      router.addRoutes(fmtRoutes);
      store.commit(‘initMenu‘, fmtRoutes);
    }
  })
}

export const formatRoutes = (routes)=> {
  let fmRoutes = [];
  routes.forEach(router=> {
    let {
      path,
      component,
      name,
      meta,
      iconCls,
      children
    } = router;

    if (children && children instanceof Array) {
      children = formatRoutes(children);
    }
    
    let fmRouter = {
      path: path,
      component(resolve){
        if (component.startsWith("Home")) {
          require([‘../components/‘ + component + ‘.vue‘], resolve)
        } else if (component.startsWith("Emp")) {
          require([‘../components/emp/‘ + component + ‘.vue‘], resolve)
        } else if (component.startsWith("Per")) {
          require([‘../components/personnel/‘ + component + ‘.vue‘], resolve)
        } else if (component.startsWith("Sal")) {
          require([‘../components/salary/‘ + component + ‘.vue‘], resolve)
        } else if (component.startsWith("Sta")) {
          require([‘../components/statistics/‘ + component + ‘.vue‘], resolve)
        } else if (component.startsWith("Sys")) {
          require([‘../components/system/‘ + component + ‘.vue‘], resolve)
        }
      },
      name: name,
      iconCls: iconCls,
      meta: meta,
      children: children
    };
    fmRoutes.push(fmRouter);
  })
  return fmRoutes;
}

调用

技术图片

 

 面包栏中遇到的问题v-text="this.$router.currentRoute.name"来获取当前路由的名字

          <el-breadcrumb separator-class="el-icon-arrow-right">
            <el-breadcrumb-item :to="{ path: ‘/home‘ }">首页</el-breadcrumb-item>
            <el-breadcrumb-item v-text="this.$router.currentRoute.name">活动管理</el-breadcrumb-item>
          </el-breadcrumb>

至此,ok.

6-项目左侧菜单栏显示

标签:for   from   element   where   遇到的问题   首页   ali   mit   进一步   

原文地址:https://www.cnblogs.com/gfbzs/p/12353261.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!