vue3+elementui-plus實現(xiàn)無限遞歸菜單示例代碼
效果圖



實現(xiàn)方式是:通過給定的數(shù)據(jù)結(jié)構(gòu)層數(shù)來動態(tài)生成多級菜單
menu.vue
<template>
<el-menu
:default-active="activeIndex"
class="el-menu-demo"
mode="horizontal"
@select="handleSelect"
background-color="#f8f8f9"
style="margin-top: 20px;margin-left: 1px;"
>
<Childrenmenu v-for="menuItem in menuItems" :key="menuItem.value" :item="menuItem" />
</el-menu>
</template>
<script setup>
import Childrenmenu from "./childrenmenu";
const menuItems = [
{
value: '1',
label: '菜單1',
children: [
{
value: '1-1',
label: '子菜單1-1',
children: [
{ value: '1-1-1', label: '子菜單1-1-1' },
{ value: '1-1-2', label: '子菜單1-1-2' },
],
},
{ value: '1-2', label: '子菜單1-2' },
],
},
{
value: '2',
label: '菜單2',
children: [
{ value: '2-1', label: '子菜單2-1' },
{
value: '2-2',
label: '子菜單2-2',
children: [
{ value: '2-2-1', label: '子菜單2-2-1' },
{ value: '2-2-2', label: '子菜單2-2-2' },
],
},
],
},
{
value: '3',
label: '菜單3',
children: [
{
value: '3-1',
label: '子菜單3-1',
children: [
{
value: '3-1-1',
label: '子菜單3-1-1',
children: [
{ value: '3-1-1-1', label: '子菜單3-1-1-1' },
{ value: '3-1-1-2', label: '子菜單3-1-1-2' },
],
},
],
},
],
},
];
const handleSelect = async (key, keyPath) => {
console.log(key, keyPath)
}
</script>childrenmenu.vue
<template>
<template v-if="item.children">
<el-sub-menu :index="item.value">
<template #title>{{ item.label }}</template>
<Childrenmenu v-for="childItem in item.children" :key="childItem.value" :item="childItem" />
</el-sub-menu>
</template>
<template v-else>
<el-menu-item :index="item.value">{{ item.label }}</el-menu-item>
</template>
</template>
<script setup>
import { defineProps } from 'vue';
const props = defineProps(['item']);
</script>
<style scoped>
</style>下面的方法可以實現(xiàn)重置菜單選項為默認項(需求場景:左側(cè)菜單切換時,上方菜單就要重置為默認選項)
<el-menu
:key="menuKey"
:default-active="activeIndex"
class="el-menu-demo"
mode="horizontal"
@select="handleSelect"
background-color="#f8f8f9"
style="margin-left: 1px;"
>
<Childrenmenu v-for="menuItem in menuItems" :key="menuItem.value" :item="menuItem" />
</el-menu>
<script setup>
const menuKey = ref(0);
//監(jiān)聽切換事件
watch(() => stationsStore.stationId, (newValue, oldValue) => {
menuKey.value += 1;
});通過給el-menu添加:key="menuKey"實現(xiàn)。
實現(xiàn)原理::key=“menuKey” 是 Vue 中的一個特殊語法,用于控制組件的重新渲染。當一個組件的 key 值發(fā)生變化時,Vue 會認為這是一個新的組件實例,會強制重新創(chuàng)建和渲染這個組件。
到此這篇關(guān)于vue3+elementui-plus實現(xiàn)無限遞歸菜單的文章就介紹到這了,更多相關(guān)vue3 elementui-plus遞歸菜單內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
vue3+ts+vite+pinia+element?plus項目使用語言國際化詳解
文章介紹了如何在Vue?3?+?TypeScript?+?Vite?+?Pinia?+?Element?Plus項目中實現(xiàn)語言國際化,主要步驟包括安裝vue-i18n、創(chuàng)建語言文件、創(chuàng)建i18n實例、全局引入、類型聲明(可選)、創(chuàng)建語言切換組件、動態(tài)加載語言文件以及在組件和PiniaStore中使用國際化2025-11-11
Vue中使用?Aplayer?和?Metingjs?添加音樂插件的方式
這篇文章主要介紹了Vue中使用?Aplayer?和?Metingjs?添加音樂插件,本文通過實例代碼給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下2022-08-08
vue指令實現(xiàn)數(shù)字和大寫中文實時互轉(zhuǎn)
這篇文章主要介紹了如何使用Vue指令實現(xiàn)在用戶輸入數(shù)字失焦后實時將數(shù)字轉(zhuǎn)為大寫中文,聚焦的時候?qū)⒋髮懼形霓D(zhuǎn)為數(shù)字以便用戶繼續(xù)修改,需要的可以參考下2024-12-12

