组件 side-menu 传入菜单数据(menuList)渲染。
1.组件 side-menu
2.menuList数据来源:
menuList() {
let arr;
if (this.tabMenu == 0) {
arr = this.demoMenu;
}
if (this.tabMenu == 1) {
arr = this.$store.getters.menuList;
}
return arr;
},
3.vuex中处理后的生成的菜单数据。
getters: {
menuList: (state, getters, rootState) =>
getMenuByRouter(routers, rootState.user.access),
},
routers,user.access 根据方法 getMenuByRouter()匹配生成新的菜单对象数组
menuList: (state, getters, rootState) =>
getMenuByRouter(routers, rootState.user.access),
const { user: { token, userId, userName } } = rootState
actions里addErrorLog获取到相关用户信息(info)存入到user:
addErrorLog ({ commit, rootState }, info)
let data = {...info,token,userId, userName}
2.routers为从后端获取到的原始对象数组,类似这种数据类型:
[
{
icon:"",
meta:{title:"用户信息",name:"user-info"},
right: undefined,size: undefined
};
{
children:[
icon: "_jinruquanping",
meta:{title: "信息报表", icon: "_jinruquanping", size: 19},
name: "info_report",
right: undefined,
size: 19,
]
icon: "_baobiaoguanli",
meta: {icon: "_baobiaoguanli", title: "报表概览", showAlways: true, size: 13, right: 8},
name: "reporting_management",
right: 8,
size: 13,
};
{
children:[
{
icon: "_jinruquanping",
meta:{title: "特殊通道", icon: "_jinruquanping", size: 19},
name: "channel_list",
right: undefined,
size: 19,
},
{
icon: "_jinruquanping",
meta:{title: "通道匹配", icon: "_jinruquanping", size: 19},
name: "channel_match",
right: undefined,
size: 19,
}
]
icon: "_celveguanli",
meta: {title: "派单发送", icon: "_celveguanli", showAlways: true, size: 14},
name: "send_management",
right: undefined,
size: 14,
};
]
判断并且匹配access,生成新的侧边栏导航带单数组。
hasOneOf 至少有一个元素包含在目标数组中
//判断要查询的数组是否至少有一个元素包含在目标数组中
export const hasOneOf = (targetarr, arr) => {
return targetarr.some(_ => arr.indexOf(_) > -1)
}
2.src/libs/util.js中getMenuByRouter方法
2.1 hasChild方法
hasChild 子节点存在且至少有一条:
export const hasChild = (item) => {
return item.children && item.children.length !== 0
}
2.2 showThisMenuEle方法
showThisMenuEle
传入的路由数组router中access和登录时获取存入的access一致时,显示菜单(返回true)
const showThisMenuEle = (item, access) => {
if (item.meta && item.meta.access && item.meta.access.length) {
if (hasOneOf(item.meta.access, access)) return true
else return false
} else return true
}
2.3 hasChild + showThisMenuEle
至少有一条时,且有access,重复遍历处理子节点
if ((hasChild(item) || (item.meta && item.meta.showAlways)) && showThisMenuEle(item, access)) {
obj.children = getMenuByRouter(item.children, access)
}
目标跳转路径等于路由跳转路径,access都存在是,存入有权限的侧边栏菜单中。
if (item.meta && item.meta.href) obj.href = item.meta.href
if (showThisMenuEle(item, access)) res.push(obj)
1.store/module/app.js中,手动修改传入的rootState.user.access
getters: {
//用户路由菜单权限处理(获取路由和登入用户权限作匹配生成新的侧边栏菜单)
//menuList: (state, getters, rootState) => getMenuByRouter(routers, rootState.user.access),
menuList: (state, getters, rootState) => getMenuByRouter(routers, "admin"),//测试权限菜单。
errorCount: state => state.errorList.length
},
2.在routers中
修改meta下的access用户角色数组。access: [‘adminSuper’]
children: [{
path: 'rest_list',
name: rest_list',
meta: {
title: '其他列表',
icon: '_jinruquanping',
size: 19,
access: ['adminSuper']
},
component: () =>
import ("@/view/policy_management/rest_list.vue")
},
]
3.启动项目,显示页面,不同用户左侧菜单有不同的菜单显示效果,测试有效。
1.main.vue中的side-menu组件
import SideMenu from "./components/side-menu";
export default {
name: "Main",
components: {
SideMenu,
}
}
a.side-menu.vue
b.side-menu.less
.side-menu-wrapper{
user-select: none;
.menu-collapsed{
padding-top: 10px;
.ivu-dropdown{
width: 100%;
.ivu-dropdown-rel a{
width: 100%;
}
}
.ivu-tooltip{
width: 100%;
.ivu-tooltip-rel{
width: 100%;
}
.ivu-tooltip-popper .ivu-tooltip-content{
.ivu-tooltip-arrow{
border-right-color: #fff;
}
.ivu-tooltip-inner{
background: #fff;
color: #495060;
}
}
}
}
a.drop-menu-a{
display: inline-block;
padding: 6px 15px;
width: 100%;
text-align: center;
color: #495060;
}
}
.menu-title{
padding-left: 6px;
margin-left: -20px;
}
2.getMenuByRouter方法
src/libs/util.js中getMenuByRouter方法
/**
* @param {Array} list 通过路由列表得到菜单列表
* @returns {Array}
*/
export const getMenuByRouter = (list, access) => {
let res = []
forEach(list, item => {
if (!item.meta || (item.meta && !item.meta.hideInMenu)) {
let obj = {
icon: (item.meta && item.meta.icon) || '',
name: item.name,
meta: item.meta,
size: (item.meta && item.meta.size) || undefined,
right: (item.meta && item.meta.right) || undefined
}
if ((hasChild(item) || (item.meta && item.meta.showAlways)) && showThisMenuEle(item, access)) {
obj.children = getMenuByRouter(item.children, access)
}
if (item.meta && item.meta.href) obj.href = item.meta.href
if (showThisMenuEle(item, access)) res.push(obj)
}
})
return res
}