vue+el-menu實現菜單欄無限多層級分類
更新時間:2022年03月28日 11:23:22 作者:lvan找不到bug
這篇文章主要為大家詳細介紹了vue+el-menu實現菜單欄無限多層級分類,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
本文實例為大家分享了vue+el-menu實現菜單欄無限多層級分類的具體代碼,供大家參考,具體內容如下
思路:數據格式須為數組內部多層嵌套模式,利用遞歸渲染菜單欄數據實現菜單多層級分類。
1.模擬菜單數據,引入封裝組件
<template>
? <div class="container">
? ? <el-container>
? ? ? <el-header>Header</el-header>
? ? ? <el-container class="container-body">
? ? ? ? <el-aside class="menu-container">
?
? ? ? ? ? <!-- 實現菜單多級分類 -->
? ? ? ? ? <el-menu
? ? ? ? ? ? default-active="1-1-1-1"
? ? ? ? ? ? background-color="#545c64"
? ? ? ? ? ? text-color="#fff"
? ? ? ? ? ? active-text-color="#ffd04b">
? ? ? ? ? ? <!-- 引入組件 -->
? ? ? ? ? ? <menu-tree :menuData="menuList"></menu-tree>
? ? ? ? ? </el-menu>
?
? ? ? ? </el-aside>
? ? ? ? <el-main class="main-container">Main</el-main>
? ? ? </el-container>
? ? </el-container>
? </div>
</template>
?
<script>
import MenuTree from '../../components/MentTree'
export default {
? components: {
? ? MenuTree
? },
? data () {
? ? return {
? ? ? menuList: [
? ? ? ? {
? ? ? ? ? index: '1',
? ? ? ? ? icon: 'el-icon-menu',
? ? ? ? ? name: '一級菜單01',
? ? ? ? ? children: [
? ? ? ? ? ? {
? ? ? ? ? ? ? index: '1-1',
? ? ? ? ? ? ? icon: 'el-icon-film',
? ? ? ? ? ? ? name: '二級菜單01',
? ? ? ? ? ? ? children: [
? ? ? ? ? ? ? ? {
? ? ? ? ? ? ? ? ? index: '1-1-1',
? ? ? ? ? ? ? ? ? icon: 'el-icon-date',
? ? ? ? ? ? ? ? ? name: '三級菜單01',
? ? ? ? ? ? ? ? ? children: [
? ? ? ? ? ? ? ? ? ? {
? ? ? ? ? ? ? ? ? ? ? index: '1-1-1-1',
? ? ? ? ? ? ? ? ? ? ? icon: 'el-icon-monitor',
? ? ? ? ? ? ? ? ? ? ? name: '四級菜單01'
? ? ? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? ? ]
? ? ? ? ? ? ? ? },
? ? ? ? ? ? ? ? {
? ? ? ? ? ? ? ? ? index: '1-1-2',
? ? ? ? ? ? ? ? ? icon: 'el-icon-headset',
? ? ? ? ? ? ? ? ? name: '三級菜單02'
? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ]
? ? ? ? ? ? },
? ? ? ? ? ? {
? ? ? ? ? ? ? index: '1-2',
? ? ? ? ? ? ? icon: 'el-icon-data-line',
? ? ? ? ? ? ? name: '二級菜單02'
? ? ? ? ? ? }
? ? ? ? ? ]
? ? ? ? },
? ? ? ? {
? ? ? ? ? index: '2',
? ? ? ? ? icon: 'el-icon-s-data',
? ? ? ? ? name: '一級菜單02'
? ? ? ? },
? ? ? ? {
? ? ? ? ? index: '3',
? ? ? ? ? icon: 'el-icon-s-operation',
? ? ? ? ? name: '一級菜單03'
? ? ? ? },
? ? ? ? {
? ? ? ? ? index: '4',
? ? ? ? ? icon: 'el-icon-user',
? ? ? ? ? name: '一級菜單04'
? ? ? ? }
? ? ? ]
? ? }
? },
? mounted () {},
? methods: {}
}
</script>2.MenuTree組件內部實現菜單欄遞歸渲染
<template>
? <div>
? ? <template v-for="menu in this.menuData">
? ? ? <el-submenu :key="menu.index" :index="menu.index" v-if="menu.children">
? ? ? ? ? <template slot="title">
? ? ? ? ? ? ? <i :class="menu.icon"></i>
? ? ? ? ? ? ? <span slot="title">{{menu.name}}</span>
? ? ? ? ? </template>
? ? ? ? ? <menu-tree :menuData="menu.children"></menu-tree>
? ? ? </el-submenu>
? ? ? <el-menu-item :key="menu.index" :index="menu.index" v-else>
? ? ? ? ? <i :class="menu.icon"></i>
? ? ? ? ? <span slot="title">{{menu.name}}</span>
? ? ? </el-menu-item>
? ? </template>
? </div>
</template>
?
<script>
export default {
? props: ['menuData'],
? name: 'MenuTree'
}
</script>3.完成效果展示

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
相關文章
vue-cli4.x創(chuàng)建企業(yè)級項目的方法步驟
這篇文章主要介紹了vue-cli4.x創(chuàng)建企業(yè)級項目的方法步驟,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2020-06-06
vue3中使用highlight.js實現代碼高亮顯示的代碼示例
代碼高亮是在網頁開發(fā)中常見的需求之一,它可以使代碼在頁面上以不同的顏色或樣式進行突出顯示提高可讀性,這篇文章主要介紹了vue3中使用highlight.js實現代碼高亮顯示的相關資料,需要的朋友可以參考下2025-04-04
Composition API思想封裝NProgress示例詳解
這篇文章主要為大家介紹了Composition API思想封裝NProgress示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2022-08-08

