vue3+elementui-plus實(shí)現(xiàn)無限遞歸菜單示例代碼
效果圖
實(shí)現(xiàn)方式是:通過給定的數(shù)據(jù)結(jié)構(gòu)層數(shù)來動(dòng)態(tài)生成多級(jí)菜單
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>
下面的方法可以實(shí)現(xiàn)重置菜單選項(xiàng)為默認(rèn)項(xiàng)(需求場景:左側(cè)菜單切換時(shí),上方菜單就要重置為默認(rèn)選項(xiàng))
<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"實(shí)現(xiàn)。
實(shí)現(xiàn)原理::key=“menuKey” 是 Vue 中的一個(gè)特殊語法,用于控制組件的重新渲染。當(dāng)一個(gè)組件的 key 值發(fā)生變化時(shí),Vue 會(huì)認(rèn)為這是一個(gè)新的組件實(shí)例,會(huì)強(qiáng)制重新創(chuàng)建和渲染這個(gè)組件。
到此這篇關(guān)于vue3+elementui-plus實(shí)現(xiàn)無限遞歸菜單的文章就介紹到這了,更多相關(guān)vue3 elementui-plus遞歸菜單內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Vue實(shí)現(xiàn)模糊查詢filter()實(shí)例詳解
因?yàn)榻赵趯W(xué)習(xí)并使用VUE,客戶有一個(gè)要求,要輸入框可模糊查詢并帶有下拉提示的應(yīng)用,數(shù)據(jù)從接口取,下面這篇文章主要給大家介紹了關(guān)于Vue實(shí)現(xiàn)模糊查詢filter()的相關(guān)資料,需要的朋友可以參考下2023-04-04vue中子組件向父組件傳遞數(shù)據(jù)的實(shí)例代碼(實(shí)現(xiàn)加減功能)
這篇文章主要介紹了vue中子組件向父組件傳遞數(shù)據(jù)的實(shí)例代碼(實(shí)現(xiàn)加減功能) ,需要的朋友可以參考下2018-04-04ElementUI 修改默認(rèn)樣式的幾種辦法(小結(jié))
這篇文章主要介紹了ElementUI 修改默認(rèn)樣式的幾種辦法(小結(jié)),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-07-07vue.js使用v-if實(shí)現(xiàn)顯示與隱藏功能示例
這篇文章主要介紹了vue.js使用v-if實(shí)現(xiàn)顯示與隱藏功能,結(jié)合簡單實(shí)例形式分析了使用v-if進(jìn)行判斷實(shí)現(xiàn)元素的顯示與隱藏功能,需要的朋友可以參考下2018-07-07webpack 3 + Vue2 使用dotenv配置多環(huán)境的步驟
這篇文章主要介紹了webpack 3 + Vue2 使用dotenv配置多環(huán)境,env文件在配置文件都可以用, vue頁面用的時(shí)候需要在 webpack.base.conf.js 重新配置,需要的朋友可以參考下2023-11-11vue3實(shí)現(xiàn)監(jiān)聽store中state狀態(tài)變化的簡單方法
這篇文章主要給大家介紹了關(guān)于vue3實(shí)現(xiàn)監(jiān)聽store中state狀態(tài)變化的簡單方法,store是一個(gè)狀態(tài)管理工具,文中通過代碼介紹的非常詳細(xì),需要的朋友可以參考下2023-10-10Vue Router之router.push和router.resolve頁面跳轉(zhuǎn)方式
這篇文章主要介紹了Vue Router之router.push和router.resolve頁面跳轉(zhuǎn)方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2025-04-04