亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

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

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

基于自建json數(shù)據(jù)的動態(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 這個文件 

遍歷數(shù)組中的menulist 中的json數(shù)據(jù),因為只遍歷到第二層 因此只支持兩層目錄 即父-子

<template>
  <div class="common-layout">
    <--! 基于elementplus的側(cè)邊欄標簽-->
        <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ù) 實現(xiàn)動態(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()  方法被一個 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 實現(xiàn)響應式數(shù)據(jù) 數(shù)據(jù)實時顯示  將后端接受的數(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;
  /* 居中對齊彈性盒子的各項 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;
}
//標簽換行樣式 vue3中不支持標簽頁換行
.text-wrapper {
  display: inline-block;
  word-break: break-all;
  word-wrap: break-word
}
</style>

效果圖

總結(jié) 

到此這篇關(guān)于vue3實現(xiàn)動態(tài)側(cè)邊菜單欄的幾種方式的文章就介紹到這了,更多相關(guān)vue3動態(tài)側(cè)邊菜單欄實現(xiàn)內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • Vue項目配置@別名全過程

    Vue項目配置@別名全過程

    這篇文章主要介紹了Vue項目配置@別名全過程,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教
    2023-10-10
  • Vue子組件關(guān)閉后調(diào)用刷新父組件的實現(xiàn)

    Vue子組件關(guān)閉后調(diào)用刷新父組件的實現(xiàn)

    這篇文章主要介紹了Vue子組件關(guān)閉后調(diào)用刷新父組件的實現(xiàn)方式,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2023-03-03
  • Vue中Keep-Alive緩存組件使用語法及原理深度解析

    Vue中Keep-Alive緩存組件使用語法及原理深度解析

    keep-alive是vue中的內(nèi)置組件,能在組件切換過程中將狀態(tài)保留在內(nèi)存中,防止重復渲染DOM,這篇文章主要介紹了Vue中Keep-Alive緩存組件使用語法及原理,需要的朋友可以參考下
    2024-07-07
  • Vue的Options用法說明

    Vue的Options用法說明

    這篇文章主要介紹了Vue的Options用法說明,具有很好的參考價值,希望對大家有所
    2020-08-08
  • vue通信方式EventBus的實現(xiàn)代碼詳解

    vue通信方式EventBus的實現(xiàn)代碼詳解

    這篇文章主要介紹了vue通信方法EventBus的實現(xiàn)代碼,本文給大家介紹的非常詳細,具有一定的參考借鑒價值,需要的朋友可以參考下
    2019-06-06
  • Vue3內(nèi)置組件Teleport使用方法詳解

    Vue3內(nèi)置組件Teleport使用方法詳解

    這篇文章主要介紹了Vue3內(nèi)置組件Teleport使用方法,Teleport是Vue 3.0 新增的一個內(nèi)置組件,主要是為了解決一些特殊場景下模態(tài)對話框組件、組件的渲染,帶著些許的了解一起走進下面文章的詳細內(nèi)容吧
    2021-10-10
  • Vue實現(xiàn)手機號、驗證碼登錄(60s禁用倒計時)

    Vue實現(xiàn)手機號、驗證碼登錄(60s禁用倒計時)

    這篇文章主要介紹了Vue實現(xiàn)手機號、驗證碼登錄(60s禁用倒計時),幫助大家更好的理解和使用vue,感興趣的朋友可以了解下
    2020-12-12
  • vue項目如何分環(huán)境部署

    vue項目如何分環(huán)境部署

    這篇文章主要介紹了vue項目如何分環(huán)境部署問題,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教
    2018-12-12
  • vue3+element?Plus使用el-tabs標簽頁解決頁面刷新不回到默認頁的問題

    vue3+element?Plus使用el-tabs標簽頁解決頁面刷新不回到默認頁的問題

    這篇文章主要介紹了vue3+element?Plus使用el-tabs標簽頁頁面刷新不回到默認頁的操作方法,本文通過實例代碼給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2023-07-07
  • Iview Table組件中各種組件擴展的使用

    Iview Table組件中各種組件擴展的使用

    這篇文章主要介紹了Iview Table組件中各種組件擴展的使用,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2018-10-10

最新評論