效果圖如下:
實現代碼如下:
<div class="sidebar">
<el-menu class="sidebar-el-menu" :default-active="onRoutes" :collapse="collapse" background-color="#324157"
text-color="#bfcbd9" active-text-color="#20a0ff" unique-opened router>
<!-- 第一層 -->
<template v-for="item in items">
<!-- 如果第一層有子菜單,則繼續循環 -->
<template v-if="item.subs">
<el-submenu :index="item.index" :key="item.index">
<template slot="title">
<i :class="item.icon"></i>
<span slot="title">{{ item.title }}</span>
</template>
<!-- 第二層 -->
<template v-for="subItem in item.subs">
<!-- 如果第二層有子菜單,則繼續循環 -->
<template v-if="subItem.subs">
<el-submenu :index="subItem.index" :key="subItem.index">
<template slot="title">
<i :class="item.icon"></i>
<span slot="title">{{ subItem.title }}</span>
</template>
<!-- <el-menu-item v-for="(threeItem,i) in subItem.subs" :key="i" :index="threeItem.index">{{ threeItem.title }}</el-menu-item> -->
<!-- 第三層 -->
<template v-for="subItem2 in subItem.subs">
<!-- 如果第三層有子菜單,則繼續循環 -->
<template v-if="subItem2.subs">
<el-submenu :index="subItem2.index" :key="subItem2.index">
<template slot="title">
<i :class="item.icon"></i>
<span slot="title">{{ subItem2.title }}</span>
</template>
<!-- <el-menu-item v-for="(fourItem,i) in subItem2.subs" :key="i" :index="fourItem.index">{{ fourItem.title }}</el-menu-item> -->
<!-- 第四層 -->
<template v-for="subItem3 in subItem2.subs">
<!-- 如果第四層有子菜單,則繼續循環 -->
<template v-if="subItem3.subs">
<el-submenu :index="subItem3.index" :key="subItem3.index">
<template slot="title">
<!-- <i :class="item.icon"></i> -->
<!-- <span slot="title">{{ subItem2.title }}</span> -->
{{ subItem3.title }}
</template>
<el-menu-item v-for="(fiveItem,i) in subItem3.subs" :key="i" :index="fiveItem.index">{{ fiveItem.title }}</el-menu-item>
</el-submenu>
</template>
<!-- 如果第四層沒有子菜單 -->
<el-menu-item v-else :index="subItem3.index" :key="subItem3.index">{{ subItem3.title }}</el-menu-item>
</template>
</el-submenu>
</template>
<!-- 如果第三層沒有子菜單 -->
<el-menu-item v-else :index="subItem2.index" :key="subItem2.index">{{ subItem2.title }}</el-menu-item>
</template>
</el-submenu>
</template>
<!-- 如果第二層沒有子菜單 -->
<el-menu-item v-else :index="subItem.index" :key="subItem.index">{{ subItem.title }}</el-menu-item>
</template>
</el-submenu>
</template>
<!-- 如果第一層沒有子菜單 -->
<template v-else>
<el-menu-item :index="item.index" :key="item.index">
<i :class="item.icon"></i>
<span slot="title">{{ item.title }}</span>
</el-menu-item>
</template>
</template>
</el-menu>
</div>
</template>
<script>
import bus from '../common/bus';
export default {
data() {
return {
collapse: false,
items: [{
icon: 'el-icon-lx-home',
index: 'dashboard',
title: '系統首頁'
},
{
icon: 'el-icon-lx-cascades',
index: 'table',
title: '基礎表格'
},
{
icon: 'el-icon-lx-copy',
index: 'tabs',
title: 'tab選項卡'
},
{
icon: 'el-icon-lx-calendar',
index: '3',
title: '表單相關',
subs: [{
index: 'form',
title: '基本表單'
},
{
index: '3-2',
title: '三級菜單',
subs: [{
index: 'editor',
title: '富文本編輯器'
},
{
index: 'markdown',
title: 'markdown編輯器'
},
{
index: '3-2-1',
title: '四級菜單',
subs: [{
index: 'music',
title: '網易雲時間'
},
{
index:'math',
title:'數學時間'
},{
index: '3-2-1-1',
title: '五級菜單',
subs: [{
index:'english',
title:'英語時間'
}, {
index:'show',
title:'展示時間'
}]
}]
}
]
},
{
index: 'upload',
title: '文件上傳'
}
]
},
{
icon: 'el-icon-lx-emoji',
index: 'icon',
title: '自定義圖標'
},
{
icon: 'el-icon-pie-chart',
index: 'charts',
title: 'schart圖表'
},
{
icon: 'el-icon-rank',
index: '6',
title: '拖拽組件',
subs: [{
index: 'drag',
title: '拖拽列表'
},
{
index: 'dialog',
title: '拖拽彈框'
}
]
},
{
icon: 'el-icon-lx-global',
index: 'i18n',
title: '國際化功能'
},
{
icon: 'el-icon-lx-warn',
index: '7',
title: '錯誤處理',
subs: [{
index: 'permission',
title: '權限測試'
},
{
index: '404',
title: '404頁面'
}
]
},
{
icon: 'el-icon-lx-redpacket_fill',
index: '/donate',
title: '支持作者'
}
]
};
},
computed: {
onRoutes() {
return this.$route.path.replace('/', '');
}
},
// created() {
// // 通過 Event Bus 進行組件間通信,來折疊側邊欄,這里接收的是header.vue那邊的數據collapse
// bus.$on('collapse', msg => {
// console.log("sidebar.vue",msg);
// this.collapse = msg;
// // 這里是向home.vue發送數據collapse-content
// bus.$emit('collapse-content', msg);
// });
// }
};
</script>
<style scoped>
.sidebar {
display: block;
position: absolute;
left: 0;
top: 70px;
bottom: 0;
overflow-y: scroll;
}
.sidebar::-webkit-scrollbar {
width: 0;
}
.sidebar-el-menu:not(.el-menu--collapse) {
width: 250px;
}
.sidebar>ul {
height: 100%;
}
</style>```