欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

vue3實(shí)現(xiàn)動(dòng)態(tài)側(cè)邊菜單欄的幾種方式簡單總結(jié)

 更新時(shí)間:2024年02月22日 08:48:51   作者:村長在路上  
在做開發(fā)中都會(huì)遇到的需求,每個(gè)用戶的權(quán)限是不一樣的,那他可以訪問的頁面(路由)可以操作的菜單選項(xiàng)是不一樣的,如果由后端控制,我們前端需要去實(shí)現(xiàn)動(dòng)態(tài)路由,動(dòng)態(tài)渲染側(cè)邊菜單欄,這篇文章主要給大家介紹了關(guān)于vue3實(shí)現(xiàn)動(dòng)態(tài)側(cè)邊菜單欄的幾種方式,需要的朋友可以參考下

基于自建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">
                  <!--一級(jí)模板區(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可以是對(duì)象也可以是變量 reactive中必須是對(duì)象。。

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">
                  <!--一級(jí)模板區(qū)域 -->
                  <template #title>
                    <el-icon class="item.iconCls"/>
                    <span>{{ item.name}}</span>
                  </template>
                  <!-- 二級(jí)目錄遍歷 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;
  /* 居中對(duì)齊彈性盒子的各項(xiàng) div 元素*/
  align-items: center;

}

.mainTitle{
  /* 規(guī)定元素中文本的水平對(duì)齊方式 居中*/
  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)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評(píng)論