<template>
<div>
<el-menu :collapse="Collapse" :collapse-transition="false" unique-opened>
<template v-for="item in MenuList">
<el-menu-item
v-if="!item.Children"
:index="item.Path"
v-permission="{'role':item.Auth,'auth':Auth}"
:key="item.Path"
>
<i class="iconfont fa-el-icon" :class="item.Icon"></i>
<span slot="title">{{item.Name}}</span>
</el-menu-item>
<el-submenu
v-else
:index="item.Path"
v-permission="{'role':item.Auth,'auth':Auth}"
:key="item.Path"
>
<template slot="title">
<i class="iconfont fa-el-icon" :class="item.Icon"></i>
<span slot="title">{{item.Name}}</span>
</template>
<template v-for="child in item.Children">
<el-menu-item
v-if="!child.Children"
:index="child.Path"
v-permission="{'role':child.Auth,'auth':Auth}"
:key="child.Path"
>
<i class="iconfont fa-el-icon" :class="child.Icon"></i>
<span slot="title">{{child.Name}}</span>
</el-menu-item>
<el-submenu
v-else
:index="child.Path"
v-permission="{'role':child.Auth,'auth':Auth}"
:key="child.Path"
>
<template slot="title">
<i class="iconfont fa-el-icon" :class="child.Icon"></i>
<span slot="title">{{child.Name}}</span>
</template>
<template v-for="grandson in child.Children">
<el-menu-item
:index="grandson.Path"
v-permission="{'role':grandson.Auth,'auth':Auth}"
:key="grandson.Path"
>
<i class="iconfont fa-el-icon" :class="grandson.Icon"></i>
<span slot="title">{{grandson.Name}}</span>
</el-menu-item>
</template>
</el-submenu>
</template>
</el-submenu>
</template>
</el-menu>
</div>
</template>
<script>
var menu = [{
Name: "首页",
Icon: "el-icon-menu",
Path: "/index",
Auth: ["index"],
Children: [{
Name: "首页1",
Path: "/index/1",
Auth: ["index"]
}]
}, {
Name: "管理",
Path: "/manger",
Icon: "el-icon-menu",
Auth: ["article", "news"],
Children: [{
Name: "管理文章",
Path: "/manger/article",
Auth: ["article"]
}, {
Name: "管理新闻",
Path: "/manger/news",
Auth: ["news"]
}]
}];
export default {
data(){
return {
MenuList: menu,
Collapse: false,
Auth: ["article"]
}
},
directives: {
permission: function permission(el, bind) {
let status = false;
for (var index = 0; index < bind.value.role.length; index++) {
var element = bind.value.role[index];
if (bind.value.auth.includes(element)) {
console.log(element);
status = true;
break;
}
}
if (!status) {
el.remove();
}
}
}
}
</script>
domo:https://codepen.io/lzq920/pen/OqOZPG