element多級(jí)菜單動(dòng)態(tài)顯示的實(shí)現(xiàn)
背景:根據(jù)后端返回?cái)?shù)據(jù)生成多級(jí)菜單,菜單項(xiàng)可能會(huì)有很深的層級(jí),如果直接使用elementUI 去編寫會(huì)寫很深的層級(jí),代碼繁雜,一旦后面菜單項(xiàng)有改動(dòng)又不利于維護(hù)
如何做到多級(jí)菜單?使用遞歸組件
elmentUI原本寫法:
<el-menu
default-active="2"
class="el-menu-vertical-demo"
@open="handleOpen"
@close="handleClose"
background-color="#545c64"
text-color="#fff"
active-text-color="#ffd04b">
<el-submenu index="1">
<template slot="title">
<i class="el-icon-location"></i>
<span>導(dǎo)航一</span>
</template>
<el-submenu index="1-4">
<template slot="title">選項(xiàng)4</template>
<el-menu-item index="1-4-1">選項(xiàng)1</el-menu-item>
</el-submenu>
</el-submenu>
<el-menu-item index="4">
<i class="el-icon-setting"></i>
<span slot="title">導(dǎo)航四</span>
</el-menu-item>
</el-menu>使用遞歸組件寫法:
主要思路:
- 通過(guò)數(shù)據(jù)查找hasOneChild()判斷是否有children,有證明有子菜單,有子菜單使用el-submenu封裝的組件
- SidebarItem.vue組件內(nèi)部調(diào)用自己的組件;
- 渲染元素組件Item.vue使用函數(shù)式組件寫法;
<template v-if="hasOneChild(item.children, item) && (!oneChild.children || oneChild.noShowChild)">
<app-link v-if="item.redirect != 'noRedirect' && item.meta" :to="resolvePath(item.path)">
<el-menu-item :index="resolvePath(item.path)" class="submenu-title-noDropdown">
<Item v-if="item.meta" :icon="item.meta.icon" :title="item.meta.title"></Item>
</el-menu-item>
</app-link>
</template>
<!-- 有子菜單:有多個(gè)children -->
<el-submenu v-else :index="resolvePath(item.path)">
<template slot="title" v-if="item.meta">
<!-- 沒(méi)有組件數(shù)據(jù)要處理可以使用函數(shù)式組件進(jìn)行渲染 -->
<Item :icon="item.meta.icon" :title="item.meta.title"></Item>
</template>
<!-- el-submenu里面還有多級(jí)菜單 -->
<sidebar-item
v-for="child in item.children"
:key="child.path"
:item="child"
:base-path="resolvePath(child.path)"
class="nest-menu"
></sidebar-item>
</el-submenu> // 判斷當(dāng)前菜單是否有子菜單
hasOneChild(children = [], item) {
// 判斷如果菜單是隱藏直接不顯示
// if(item.hidden) return false;
if (children.length === 0) return false;
const showChildArr = children.filter(child => {
console.log(child);
if (child.hidden) return false;
else return true;
});
// 沒(méi)有找到child說(shuō)明沒(méi)有子菜單
if (showChildArr.length === 0) {
this.oneChild = { ...item, path: item.path, noShowChild: true };
return true;
}
console.log(this.oneChild, "this.oneChild");
return false;
},函數(shù)式組件Item.vue寫法:
- functional:true定義組件為函數(shù)式組件;
- props接收父級(jí)傳入屬性;
- render函數(shù)生成虛擬節(jié)點(diǎn);
<script>
export default {
name: "Item",
functional: true,
// 組件傳入的數(shù)據(jù)
props: {
title: {
type: String,
default: ""
},
icon: {
type: String,
default: ""
}
},
// render函數(shù)生成虛擬節(jié)點(diǎn)
render(h, context) {
// <i class="iconfont el-icon-location"></i>
// <span>{{item.meta.title}}</span>
const { title, icon } = context.props; //獲取傳入的屬性
const vNode = [];
if (icon) {
const iconClass = `iconfont ` + icon;
vNode.push(<i class={iconClass} style="font-size:18px;"></i>);
}
if (title) {
// JSX語(yǔ)法
vNode.push(<span style="margin-left:8px;">{title}</span>);
}
return vNode;
}
};
</script>到此這篇關(guān)于elment多級(jí)菜單動(dòng)態(tài)顯示的實(shí)現(xiàn)的文章就介紹到這了,更多相關(guān)elment多級(jí)菜單動(dòng)態(tài)顯示內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
vue項(xiàng)目中使用qrcodesjs2生成二維碼簡(jiǎn)單示例
最近項(xiàng)目中需生成二維碼,發(fā)現(xiàn)了很好用的插件qrcodesjs2,所以下面這篇文章主要給大家介紹了關(guān)于vue項(xiàng)目中使用qrcodesjs2生成二維碼的相關(guān)資料,文中通過(guò)實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下2023-05-05
解決基于 keep-alive 的后臺(tái)多級(jí)路由緩存問(wèn)題
這篇文章主要介紹了解決基于 keep-alive 的后臺(tái)多級(jí)路由緩存問(wèn)題,本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-12-12
elementUI自定義上傳文件功能實(shí)現(xiàn)(前端后端超詳細(xì)過(guò)程)
自定義上傳思路很簡(jiǎn)單,下面這篇文章主要給大家介紹了關(guān)于elementUI自定義上傳文件功能實(shí)現(xiàn)(前端后端超詳細(xì)過(guò)程)的相關(guān)資料,文中通過(guò)實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下2022-11-11
vue toggle做一個(gè)點(diǎn)擊切換class(實(shí)例講解)
下面小編就為大家分享一篇使用vue toggle實(shí)現(xiàn)點(diǎn)擊切換class的示例。具有很好的參考價(jià)值。希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2018-03-03
基于Vue2實(shí)現(xiàn)數(shù)字縱向滾動(dòng)效果
這篇文章主要為大家詳細(xì)介紹了如何基于Vue2實(shí)現(xiàn)數(shù)字縱向滾動(dòng)效果,從而達(dá)到顯示計(jì)時(shí)器滾動(dòng)效果,感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下2024-03-03
淺析Proxy可以優(yōu)化vue的數(shù)據(jù)監(jiān)聽機(jī)制問(wèn)題及實(shí)現(xiàn)思路
這篇文章主要介紹了淺析Proxy可以優(yōu)化vue的數(shù)據(jù)監(jiān)聽機(jī)制問(wèn)題及實(shí)現(xiàn)思路,需要的朋友可以參考下2018-11-11

