子组件:
<template>
<div>
<template v-for="(item,index) in menuList">
<!-- 有次级菜单的,则展开子选项-->
<el-submenu v-if="item.children && item.children.length>0" :key="index" :index="item.id">
<template #title>
<!-- <el-icon v-if="item.icon!=''"><component :is="item.icon" /></el-icon> -->
<span >{{item.name}}</span>
</template>
<!-- 递归,实现无限级展开 -->
<MenuTree :menuList="item.children" @childEvent="chosedMenu" :key="index" ></MenuTree>
</el-submenu>
<!-- 没有次级菜单的 -->
<el-menu-item v-if="!item.children" :key="index" @click="routerGo(item,index)" :index='item.remark' :class="activeMenu==index?'activeMenu':''" >
<!-- <el-icon v-if="item.icon!=''"><component :is="item.icon" /></el-icon> -->
<span >{{item.name}}</span>
</el-menu-item>
</template>
</div>
</template>
<script>
export default {
name: 'MenuTree',
props:{
menuList:{
type:Array,
default(){
return []
}
}
},
data(){
return{
chosed:'',
}
},
mounted(){
// console.log(this.menuList,'123');
// this.$emit('childEvent',i)
},
methods: {
// 菜单点击的操作
routerGo(i,index){
console.log(i,index);
this.$emit('childEvent',i)
this.chosed = index
},
// 接受本身组件传来的参数,然后将传来得值,发送到外层父组件
chosedMenu(i){
this.$emit('childEvent',i)
},
}
}
</script>
<style scoped lang='scss'>
.activeMenu{
color: #fff !important;
background: #2055b0 !important;
}
/* 菜单栏选中后背景色 */
.el-menu-item {
color: #ffffff;
}
.el-menu-item.is-active {
color: #fff;
background: #2055b0 !important;
}
/*选中菜单后的操作*/
.is-active {
background: #2055b0 !important;
}
</style>
父组件:
<el-menu
:default-active='menuList[0].remark'
class="el-menu-vertical-demo"
text-color="rgb(195, 200, 203)"
background-color="#192654"
active-text-color="#fff"
:unique-opened="true"
>
<MenuTree :menuList="menuList" @childEvent="chosedMenu"></MenuTree>
</el-menu>
数据:
menuList:[
{
id:1,
parentid:'0',
name:'测试菜单',
icon:'HomeFilled',
remark: 'loadbusi/workSys/workOrder',
},
{
id:2,
parentid:'0',
name:'实训室',
icon:'UserFilled',
children:[
{
id:3,
parentid:'2',
name:'测试菜单1',
icon:'',
children:[
{
id:4,
parentid:'2',
name:'测试菜单1-1',
icon:'',
remark: 'loadbusi/workSys/workTotal',
}
]
},
{
id:5,
parentid:'2',
name:'测试菜单2',
icon:'',
remark: 'loadbusi/emergency/demandManage',
}
]
},
],