vue3實(shí)現(xiàn)動(dòng)態(tài)側(cè)邊菜單欄的幾種方式簡單總結(jié)
基于自建json數(shù)據(jù)的動(dòng)態(tài)側(cè)邊菜單欄
后端接口json數(shù)據(jù)
src/api/menuList.js
const menuList = [
{
url: '',
name: '人員管理',
icon: 'icon-renyuan',
menuId: 1,
children: [
{
url: '/user',
name: '用戶管理',
icon: 'icon-jurassic_user',
menuId: 1001,
children: []
},
{
url: '/role',
name: '角色管理',
icon: 'icon-jiaose',
menuId: 1002,
children: []
}
]
},
{
url: '/device',
name: '設(shè)備管理',
icon: 'icon-shebei',
menuId: 2
}
]
export default menuList;
home.vue頁面上面顯示該類型的菜單
在home.vue頁面中import 這個(gè)文件
遍歷數(shù)組中的menulist 中的json數(shù)據(jù),因?yàn)橹槐闅v到第二層 因此只支持兩層目錄 即父-子
<template>
<div class="common-layout">
<--! 基于elementplus的側(cè)邊欄標(biāo)簽-->
<el-aside width="400px">
<el-row class="tac">
<el-col :span="12">
<el-menu
default-active="2"
class="el-menu-vertical-demo"
:router="true"
>
<el-sub-menu index="1" v-for="item in menuList" :key="item.id">
<!--一級模板區(qū)域 -->
<template #title>
<el-icon class="item.iconCls"/>
<span>{{ item.name}}</span>
</template>
<el-menu-item :index="'/'+subItem.path" v-for="subItem in item.children" :key="item.id">
<template #title>
<span>{{subItem.name}}</span>
</template>
</el-menu-item>
</el-sub-menu>
</el-menu>
</el-col>
</el-row>
</el-aside>
</el-container>
</el-container>
</div>
</template>
<script>
import menuList from "@/api/mockdata/menList"; //根據(jù)自己的路徑來
export default {
name: "Home",
data(){
return {
menuList
}
},
}
</script>
<style >
</style>
效果圖

基于后端接口數(shù)據(jù) 實(shí)現(xiàn)動(dòng)態(tài)側(cè)邊菜單欄 vue3+elementplus
后端菜單接口數(shù)據(jù)
目錄結(jié)構(gòu)為 父目錄 系統(tǒng)管理 子目錄 廣告管理 APP上傳。 遍歷json數(shù)據(jù) 在v-for頁面顯示 name 名稱
http://localhost:8000/api/menu
[
{
"id": 6,
"url": "/",
"path": "/home",
"component": "Home",
"name": "系統(tǒng)管理",
"iconCls": "fa fa-windows",
"enabled": true,
"children": [
{
"id": 30,
"url": null,
"path": "/sys/ad",
"component": "SysAd",
"name": "廣告管理",
"iconCls": "fa fa-ad",
"meta": null,
"parentId": 6,
"enabled": true,
"children": null,
"roles": null
},
{
"id": 7,
"url": null,
"path": "/sys/app",
"component": "SysApp",
"name": "App上傳",
"iconCls": "fa-solid fa-circle-arrow-up",
"meta": null,
"parentId": 6,
"enabled": true,
"children": null,
"roles": null
}
],
"roles": null
}
]在 home.vue中 顯示此json數(shù)據(jù)形成的菜單
vue3包含的data() mounted() methods() 方法被一個(gè) setup方法全包了
ref可以是對象也可以是變量 reactive中必須是對象。。
ref使用變量 不管是獲取還是使用 都需要加上 .value
<template>
<div class="common-layout">
<el-container>
<el-aside width="400px">
<el-row class="tac">
<el-col :span="12">
<el-menu
default-active="2"
class="el-menu-vertical-demo"
:router="true"
>
<el-sub-menu index="1" v-for="item in menuList" :key="item.id">
<!--一級模板區(qū)域 -->
<template #title>
<el-icon class="item.iconCls"/>
<span>{{ item.name}}</span>
</template>
<!-- 二級目錄遍歷 json中的children 顯示name:中的內(nèi)容-->
<el-menu-item :index="'/'+subItem.path" v-for="subItem in item.children" :key="item.id">
<template #title>
<span>{{subItem.name}}</span>
</template>
</el-menu-item>
</el-sub-menu>
</el-menu>
</el-col>
</el-row>
</el-aside>
</el-container>
</el-container>
</div>
</template>
<script>
import { Document, Location, Setting, Burger} from "@element-plus/icons-vue";
import axios from "axios";
import {onMounted,ref } from 'vue'
import {useStore} from "vuex";
export default {
name: "Home",
components: {Burger, Setting, Document, Location},
setup() {
// vue3推薦使用ref 實(shí)現(xiàn)響應(yīng)式數(shù)據(jù) 數(shù)據(jù)實(shí)時(shí)顯示 將后端接受的數(shù)據(jù)賦值給ref
const menuList = ref();
onMounted(()=>{
axios.get("/api/menu").then(res =>{
console.log("onMounted")
const data = res.data
console.log(data)
menuList.value = data
})
})
return {
menuList
}
},
}
</script>
<style >
.homeHeader{
background-color: #04befe;
/*彈性展示*/
display: flex;
/* 居中對齊彈性盒子的各項(xiàng) div 元素*/
align-items: center;
}
.mainTitle{
/* 規(guī)定元素中文本的水平對齊方式 居中*/
text-align: center;
/* 顏色為深空色*/
color: deepskyblue;
/* 字體大小*/
font-size: 30px;
/* 距離頂部的距離為 20px 數(shù)值越大下降位置越多*/
//padding-top: 20px;
}
.headerTitle{
/*字體大小*/
font-size: 20px;
/* 字體顏色*/
color: #fffff9;
}
//標(biāo)簽換行樣式 vue3中不支持標(biāo)簽頁換行
.text-wrapper {
display: inline-block;
word-break: break-all;
word-wrap: break-word
}
</style>效果圖

總結(jié)
到此這篇關(guān)于vue3實(shí)現(xiàn)動(dòng)態(tài)側(cè)邊菜單欄的幾種方式的文章就介紹到這了,更多相關(guān)vue3動(dòng)態(tài)側(cè)邊菜單欄實(shí)現(xiàn)內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
基于Vue組件化的日期聯(lián)動(dòng)選擇器功能的實(shí)現(xiàn)代碼
這篇文章主要介紹了基于Vue組件化的日期聯(lián)動(dòng)選擇器的實(shí)現(xiàn)代碼,非常不錯(cuò),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2018-11-11
elementUI Table組件實(shí)現(xiàn)表頭吸頂效果(示例代碼)
文章介紹了如何在vue2.6+和elementUI環(huán)境下實(shí)現(xiàn)el-table組件的表頭吸頂效果,通過添加樣式、注冊指令、引入指令并在父元素中避免使用overflow:hidden,可以實(shí)現(xiàn)場景下表頭始終可見,本文通過實(shí)例代碼介紹的非常詳細(xì),感興趣的朋友一起看看吧2025-01-01
vue3+electron12+dll開發(fā)客戶端配置詳解
本文將結(jié)合實(shí)例代碼,介紹vue3+electron12+dll客戶端配置,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2021-06-06
解決Vue_localStorage本地存儲(chǔ)和本地取值問題
這篇文章主要介紹了解決Vue_localStorage本地存儲(chǔ)和本地取值問題,具有很好的參考價(jià)值,希望對大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-07-07
vue 獲取到數(shù)據(jù)但卻渲染不到頁面上的解決方法
這篇文章主要介紹了vue 獲取到數(shù)據(jù)但卻渲染不到頁面上的解決方法,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-11-11
vue結(jié)合Echarts實(shí)現(xiàn)點(diǎn)擊高亮效果的示例
下面小編就為大家分享一篇vue結(jié)合Echarts實(shí)現(xiàn)點(diǎn)擊高亮效果的示例,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-03-03

