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)重置菜單選項為默認(rèn)項(需求場景:左側(cè)菜單切換時,上方菜單就要重置為默認(rèn)選項)
<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 中的一個特殊語法,用于控制組件的重新渲染。當(dāng)一個組件的 key 值發(fā)生變化時,Vue 會認(rèn)為這是一個新的組件實例,會強制重新創(chuàng)建和渲染這個組件。
到此這篇關(guān)于vue3+elementui-plus實現(xiàn)無限遞歸菜單的文章就介紹到這了,更多相關(guān)vue3 elementui-plus遞歸菜單內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
vue中子組件向父組件傳遞數(shù)據(jù)的實例代碼(實現(xiàn)加減功能)
這篇文章主要介紹了vue中子組件向父組件傳遞數(shù)據(jù)的實例代碼(實現(xiàn)加減功能) ,需要的朋友可以參考下2018-04-04
ElementUI 修改默認(rèn)樣式的幾種辦法(小結(jié))
這篇文章主要介紹了ElementUI 修改默認(rèn)樣式的幾種辦法(小結(jié)),文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-07-07
webpack 3 + Vue2 使用dotenv配置多環(huán)境的步驟
這篇文章主要介紹了webpack 3 + Vue2 使用dotenv配置多環(huán)境,env文件在配置文件都可以用, vue頁面用的時候需要在 webpack.base.conf.js 重新配置,需要的朋友可以參考下2023-11-11
vue3實現(xiàn)監(jiān)聽store中state狀態(tài)變化的簡單方法
這篇文章主要給大家介紹了關(guān)于vue3實現(xiàn)監(jiān)聽store中state狀態(tài)變化的簡單方法,store是一個狀態(tài)管理工具,文中通過代碼介紹的非常詳細,需要的朋友可以參考下2023-10-10
Vue Router之router.push和router.resolve頁面跳轉(zhuǎn)方式
這篇文章主要介紹了Vue Router之router.push和router.resolve頁面跳轉(zhuǎn)方式,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2025-04-04

