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