element多級菜單動態(tài)顯示的實(shí)現(xiàn)
背景:根據(jù)后端返回?cái)?shù)據(jù)生成多級菜單,菜單項(xiàng)可能會有很深的層級,如果直接使用elementUI 去編寫會寫很深的層級,代碼繁雜,一旦后面菜單項(xiàng)有改動又不利于維護(hù)
如何做到多級菜單?使用遞歸組件
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>使用遞歸組件寫法:
主要思路:
- 通過數(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>
<!-- 有子菜單:有多個children -->
<el-submenu v-else :index="resolvePath(item.path)">
<template slot="title" v-if="item.meta">
<!-- 沒有組件數(shù)據(jù)要處理可以使用函數(shù)式組件進(jìn)行渲染 -->
<Item :icon="item.meta.icon" :title="item.meta.title"></Item>
</template>
<!-- el-submenu里面還有多級菜單 -->
<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;
});
// 沒有找到child說明沒有子菜單
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接收父級傳入屬性;
- 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語法
vNode.push(<span style="margin-left:8px;">{title}</span>);
}
return vNode;
}
};
</script>到此這篇關(guān)于elment多級菜單動態(tài)顯示的實(shí)現(xiàn)的文章就介紹到這了,更多相關(guān)elment多級菜單動態(tài)顯示內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
vue實(shí)現(xiàn)el-select的change事件過程
這篇文章主要介紹了vue實(shí)現(xiàn)el-select的change事件過程,具有很好的參考價(jià)值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2024-04-04
詳解keep-alive + vuex 讓緩存的頁面靈活起來
這篇文章主要介紹了keep-alive + vuex 讓緩存的頁面靈活起來,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-04-04
詳解關(guān)于element級聯(lián)選擇器數(shù)據(jù)回顯問題
這篇文章主要介紹了詳解關(guān)于element級聯(lián)選擇器數(shù)據(jù)回顯問題,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2019-02-02
ElementUI如何修改Dialog的標(biāo)題樣式
這篇文章主要介紹了ElementUI如何修改Dialog的標(biāo)題樣式問題,具有很好的參考價(jià)值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2023-10-10
詳解Vue如何進(jìn)行表單聯(lián)動與級聯(lián)選擇
表單聯(lián)動和級聯(lián)選擇是Vue.js中常見的功能,在下面的文章中,我們將討論如何在Vue.js中實(shí)現(xiàn)表單聯(lián)動和級聯(lián)選擇,感興趣的小伙伴可以了解一下2023-06-06
vue-cli3 項(xiàng)目從搭建優(yōu)化到docker部署的方法
這篇文章主要介紹了vue-cli3 項(xiàng)目從搭建優(yōu)化到docker部署的方法,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2019-01-01

