vue3+element-plus動(dòng)態(tài)路由菜單示例代碼
1、展示效果圖
2、創(chuàng)建組件 SideMenu.vue
<!-- 側(cè)邊欄組件 --> <template> <div> <el-menu :default-active="activeIndex" background-color="#001529" text-color="#ffffff"> <template v-for="(item, index) in menuList" :key="item.path"> <!-- 沒有子路由 --> <template v-if="!item.children"> <el-menu-item :index="item.path" v-if="!item.mate.hidden" @click="goPage"> <template #title> <i class="iconfont" :class="item.mate.icon"></i> <label>{{ item.mate.title }}</label> </template> </el-menu-item> </template> <!-- 只有一個(gè)子路由 --> <template v-if="item.children && item.children.length == 1"> <el-menu-item v-if="!item.children[0].mate.hidden" :index="item.children[0].path" @click="goPage"> <template #title> <i class="iconfont" :class="item.children[0].mate.icon"></i> <label>{{ item.children[0].mate.title }}</label> </template> </el-menu-item> </template> <!-- 有兩個(gè)及以上子路由 --> <el-sub-menu v-if="item.children && item.children.length > 1" :index="item.path"> <template #title> <i class="iconfont" :class="item.mate.icon"></i> <label>{{ item.mate.title }}</label> </template> <!-- 遞歸組件 --> <side-menu :menuList="item.children"></side-menu> </el-sub-menu> </template> </el-menu> </div> </template> <script setup lang="ts"> import { ref } from 'vue'; import { useRouter } from 'vue-router'; defineProps(['menuList']); const activeIndex = ref('1'); const $router = useRouter(); // 路由跳轉(zhuǎn) const goPage = (vc) => { $router.push(vc.index); }; </script> <script lang="ts"> // 當(dāng)前組件的名稱 用于遞歸組件使用 export default { name: 'SideMenu', }; </script> <style scoped lang="scss"> .el-menu { border-right: none; .iconfont { padding-right: 6px; } } </style>
3、路由示例
icon使用的是阿里巴巴圖標(biāo)庫(kù),需要下載在index.html全局引入
路由先存到store里面
創(chuàng)建routes.ts
export const constantRoute = [ { path: '/login', name: 'login', component: () => import('@/views/login/login.vue'), mate: { title: '登錄', //菜單標(biāo)題 hidden: true, //是否隱藏:true隱藏,false不隱藏,默認(rèn)hidden隱藏 }, }, { path: '/', name: 'layout', redirect: '/home', component: () => import('@/layout/index.vue'), mate: { title: 'layout', //菜單標(biāo)題 hidden: true, //是否隱藏:true隱藏,false不隱藏 icon: '', //iconfont 名稱 }, children: [ { path: '/home', name: 'home', component: () => import('@/views/home/home.vue'), mate: { title: '首頁(yè)', //菜單標(biāo)題 icon: 'icon-shouye1', //iconfont 名稱 }, }, ], }, { path: '/screen', name: 'screen', component: () => import('@/views/screen/index.vue'), mate: { title: '數(shù)據(jù)大屏', //菜單標(biāo)題 icon: 'icon-zonghefenxipingtai', }, }, { path: '/404', name: '404', component: () => import('@/views/404/index.vue'), mate: { title: '404', //菜單標(biāo)題 hidden: true, //是否隱藏:true隱藏,false不隱藏 }, }, ];
4、在頁(yè)面中使用
<el-scrollbar class="scroll-bar"> <side-menu :menuList="userStore.menuRoutes"></side-menu> </el-scrollbar> <script setup lang="ts"> import useUserStore from '@/store/modules/user'; let userStore = useUserStore(); </script>
到此這篇關(guān)于vue3+element-plus動(dòng)態(tài)路由菜單的文章就介紹到這了,更多相關(guān)vue3 element-plus路由菜單內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
VScode更新后安裝vetur仍無法格式化vue文件的解決
這篇文章主要介紹了VScode更新后安裝vetur仍無法格式化vue文件的解決方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-10-10vue3實(shí)現(xiàn)在新標(biāo)簽中打開指定網(wǎng)址的方法
我希望點(diǎn)擊查看按鈕的時(shí)候,能夠在新的標(biāo)簽頁(yè)面打開這個(gè)文件的地址進(jìn)行預(yù)覽,該如何實(shí)現(xiàn)呢,下面小編給大家?guī)砹嘶趘ue3實(shí)現(xiàn)在新標(biāo)簽中打開指定的網(wǎng)址,感興趣的朋友跟隨小編一起看看吧2024-07-07vue音樂播放器插件vue-aplayer的配置及其使用實(shí)例詳解
本篇文章主要介紹了vue音樂播放器插件vue-aplayer的配置及其使用實(shí)例詳解,具有一定的參考價(jià)值,有興趣的可以了解一下2017-07-07elementui el-table中如何給表頭 el-table-column 加一個(gè)鼠
本文通過實(shí)例代碼介紹如何在使用Element UI的el-table組件時(shí)為表頭添加提示功能,通過結(jié)合el-tooltip組件實(shí)現(xiàn)鼠標(biāo)移入時(shí)顯示提示信息,感興趣的朋友跟隨小編一起看看吧2024-11-11Vue 實(shí)現(xiàn)輸入框新增搜索歷史記錄功能
這篇文章主要介紹了Vue 輸入框新增搜索歷史記錄功能,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2019-10-10vue-cli3.0 腳手架搭建項(xiàng)目的過程詳解
這篇文章主要介紹了vue-cli3.0 腳手架搭建項(xiàng)目的過程,本文分步驟給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2018-10-10分析 Vue 中的 computed 和 watch 的區(qū)別
這篇文章分析 Vue 的 computed 和 watch 的區(qū)別,computed 用來監(jiān)控自己定義的變量,頁(yè)面上可直接使用。watch 是監(jiān)測(cè) Vue 實(shí)例上的數(shù)據(jù)變動(dòng),通俗地講,就是檢測(cè) data 內(nèi)聲明的數(shù)據(jù),需要的朋友可以參考一下2021-09-09Vue利用computed配合watch實(shí)現(xiàn)監(jiān)聽多個(gè)屬性的變化
這篇文章主要給大家介紹了在Vue中巧用computed配合watch實(shí)現(xiàn)監(jiān)聽多個(gè)屬性的變化的方法,文中有詳細(xì)的代碼示例供大家參考,具有一定的參考價(jià)值,需要的朋友可以參考下2023-10-10一篇看懂vuejs的狀態(tài)管理神器 vuex狀態(tài)管理模式
一篇看懂vuejs的狀態(tài)管理神器,Vuex一個(gè)專為Vue.js應(yīng)用程序開發(fā)的狀態(tài)管理模式,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-04-04