vue+el-menu實(shí)現(xiàn)菜單欄無(wú)限多層級(jí)分類
本文實(shí)例為大家分享了vue+el-menu實(shí)現(xiàn)菜單欄無(wú)限多層級(jí)分類的具體代碼,供大家參考,具體內(nèi)容如下
思路:數(shù)據(jù)格式須為數(shù)組內(nèi)部多層嵌套模式,利用遞歸渲染菜單欄數(shù)據(jù)實(shí)現(xiàn)菜單多層級(jí)分類。
1.模擬菜單數(shù)據(jù),引入封裝組件
<template>
? <div class="container">
? ? <el-container>
? ? ? <el-header>Header</el-header>
? ? ? <el-container class="container-body">
? ? ? ? <el-aside class="menu-container">
?
? ? ? ? ? <!-- 實(shí)現(xiàn)菜單多級(jí)分類 -->
? ? ? ? ? <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: '一級(jí)菜單01',
? ? ? ? ? children: [
? ? ? ? ? ? {
? ? ? ? ? ? ? index: '1-1',
? ? ? ? ? ? ? icon: 'el-icon-film',
? ? ? ? ? ? ? name: '二級(jí)菜單01',
? ? ? ? ? ? ? children: [
? ? ? ? ? ? ? ? {
? ? ? ? ? ? ? ? ? index: '1-1-1',
? ? ? ? ? ? ? ? ? icon: 'el-icon-date',
? ? ? ? ? ? ? ? ? name: '三級(jí)菜單01',
? ? ? ? ? ? ? ? ? children: [
? ? ? ? ? ? ? ? ? ? {
? ? ? ? ? ? ? ? ? ? ? index: '1-1-1-1',
? ? ? ? ? ? ? ? ? ? ? icon: 'el-icon-monitor',
? ? ? ? ? ? ? ? ? ? ? name: '四級(jí)菜單01'
? ? ? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? ? ]
? ? ? ? ? ? ? ? },
? ? ? ? ? ? ? ? {
? ? ? ? ? ? ? ? ? index: '1-1-2',
? ? ? ? ? ? ? ? ? icon: 'el-icon-headset',
? ? ? ? ? ? ? ? ? name: '三級(jí)菜單02'
? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ]
? ? ? ? ? ? },
? ? ? ? ? ? {
? ? ? ? ? ? ? index: '1-2',
? ? ? ? ? ? ? icon: 'el-icon-data-line',
? ? ? ? ? ? ? name: '二級(jí)菜單02'
? ? ? ? ? ? }
? ? ? ? ? ]
? ? ? ? },
? ? ? ? {
? ? ? ? ? index: '2',
? ? ? ? ? icon: 'el-icon-s-data',
? ? ? ? ? name: '一級(jí)菜單02'
? ? ? ? },
? ? ? ? {
? ? ? ? ? index: '3',
? ? ? ? ? icon: 'el-icon-s-operation',
? ? ? ? ? name: '一級(jí)菜單03'
? ? ? ? },
? ? ? ? {
? ? ? ? ? index: '4',
? ? ? ? ? icon: 'el-icon-user',
? ? ? ? ? name: '一級(jí)菜單04'
? ? ? ? }
? ? ? ]
? ? }
? },
? mounted () {},
? methods: {}
}
</script>2.MenuTree組件內(nèi)部實(shí)現(xiàn)菜單欄遞歸渲染
<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.完成效果展示

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
詳解vue.js移動(dòng)端配置flexible.js及注意事項(xiàng)
最近在用vue做移動(dòng)端項(xiàng)目,網(wǎng)上找了一些移動(dòng)端適配的方案,個(gè)人覺得手淘團(tuán)隊(duì)flexible.js還是比較容易上手,在這里做下總結(jié)。對(duì)vue.js移動(dòng)端配置flexible.js 相關(guān)知識(shí)感興趣的朋友跟隨小編一起看看吧2019-04-04
Vue3+Element-plus項(xiàng)目自動(dòng)導(dǎo)入報(bào)錯(cuò)的解決方案
vue3出來(lái)一段時(shí)間了,element也更新了版本去兼容vue3,下面這篇文章主要給大家介紹了關(guān)于Vue3+Element-plus項(xiàng)目自動(dòng)導(dǎo)入報(bào)錯(cuò)的解決方案,文中通過(guò)實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下2022-07-07
vue中的this.$refs,this.$emit,this.$store,this.$nextTick的使用方式
這篇文章主要介紹了vue中的this.$refs,this.$emit,this.$store,this.$nextTick的使用方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-04-04
詳解從零搭建 vue2 vue-router2 webpack3 工程
本篇文章主要介紹了詳解從零搭建 vue2 vue-router2 webpack3 工程,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-11-11
vue全局方法plugins/utils的實(shí)現(xiàn)示例
很多時(shí)候我們會(huì)在全局調(diào)用一些方法,本文主要介紹了vue全局方法plugins/utils的實(shí)現(xiàn)示例,具有一定的參考價(jià)值,感興趣的可以了解一下2024-07-07
Vue組件渲染與更新實(shí)現(xiàn)過(guò)程淺析
這篇文章主要介紹了Vue組件渲染與更新實(shí)現(xiàn)過(guò)程,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)吧2023-03-03

