Vue開發實例(六)實現左側菜單導航

左側菜單導航

  • 一、一級菜單
  • 二、二級菜單
  • 三、三級菜單
    • 1、加入相關事件
  • 四、菜單點擊跳轉
    • 1. 創建新頁面
    • 2. 配置路由
    • 3. 菜單中加入路由配置
    • 4、處理默認的Main窗口為空的情況
  • 五、動態左側菜單導航
    • 1、動態實現一級菜單
    • 2、動態實現二級菜單

一、一級菜單

在之前的Aside.vue中去實現代碼,一級菜單其實非常的簡單,直接用el-menu 和el-menu-item 就行,Aside.vue代碼如下:

<template><el-menu><el-menu-item>一級菜單1</el-menu-item><el-menu-item>一級菜單2</el-menu-item><el-menu-item>一級菜單3</el-menu-item></el-menu>
</template><script>
export default {name: "Aside",
};
</script><style scoped>
</style>

在這里插入圖片描述

  1. 設置菜單背景顏色和文字顏色
    • 在el-menu中設置 background-color 和 text-color 屬性
  2. 設置選中后菜單文字顏色
    • 設置 active-text-color 屬性,但是必須在需要生效的子菜單中設置index屬性,否則不生效,先不設置index
  3. 設置默認的選中菜單
    • 設置default-active為對應的index值即可
    • 比如我設置默認選中第2個菜單,第2個菜單的index為2,所以我們在el-menu中加入 default-active=“2”
  4. 在菜單中加入圖標
    • 用 i 標簽即可,在菜單名前面加入 <i class=“el-icon-XXX”>,XXX是圖標的名稱。

效果如圖所示:
在這里插入圖片描述
一級菜單全部代碼

<template><el-menubackground-color="#545c64"text-color="#ffffff"active-text-color="#ffd04b"default-active="2"><el-menu-item index="1"><i class="el-icon-location"></i>一級菜單1</el-menu-item><el-menu-item index="2"><i class="el-icon-document"></i>一級菜單2</el-menu-item><el-menu-item index="3"><i class="el-icon-setting"></i>一級菜單3</el-menu-item></el-menu>
</template><script>
export default {name: "Aside",
};
</script><style scoped>
.el-icon-location,
.el-icon-document,
.el-icon-setting {display: inline-flex;align-items: center;justify-content: center;
}
</style>

二、二級菜單

<div><el-menu background-color="#545c64" text-color="#ffffff"active-text-color="#ffd04b" default-active="2" ><el-submenu index="1"><template slot="title"><i class="el-icon-location"></i><span>一級菜單1</span></template><el-menu-item index="1-1">選項1</el-menu-item><el-menu-item index="1-2">選項2</el-menu-item></el-submenu><el-menu-item index="2"><i class="el-icon-document"></i>一級菜單2</el-menu-item><el-menu-item index="3"><i class="el-icon-setting"></i>一級菜單3</el-menu-item></el-menu></div>

修改步驟:

  1. el-menu 修改為 el-submenu
  2. 按鈕名稱、圖標用 template 標簽包裹,必須加入 slot="title"屬性,否則菜單樣式不對。
  3. 加入新的兩個 el-menu-item

在這里插入圖片描述
參考代碼:

由于之前挖過一個坑,就是global.css里面的height,之前也提到過,所以要設置一下,el-submenu的高度,具體的參考代碼

<template><div><el-menubackground-color="#545c64"text-color="#ffffff"active-text-color="#ffd04b"default-active="2"><el-submenu index="1"><template slot="title"><i class="el-icon-location"></i><span>一級菜單1</span></template><el-menu-item index="1-1">選項1</el-menu-item><el-menu-item index="1-2">選項2</el-menu-item></el-submenu><el-menu-item index="2"><i class="el-icon-document"></i>一級菜單2</el-menu-item><el-menu-item index="3"><i class="el-icon-setting"></i>一級菜單3</el-menu-item></el-menu></div>
</template><script>
export default {name: "Aside",
};
</script><style scoped>
.el-submenu {height: auto;
}
.el-icon-location,
.el-icon-document,
.el-icon-setting {display: inline-flex;align-items: center;justify-content: center;
}
</style>

三、三級菜單

跟二級菜單的修改方式是一樣的,就是多加一層

在這里插入圖片描述
參考代碼:

<template><div><el-menubackground-color="#545c64"text-color="#ffffff"active-text-color="#ffd04b"default-active="2"><el-submenu index="1"><template slot="title"><i class="el-icon-location"></i><span>一級菜單1</span></template><el-submenu index="1-1"><template slot="title"><i class="el-icon-location"></i><span>選項1</span></template><el-menu-item index="1-1-1"><i class="el-icon-document"></i>選項1-1</el-menu-item><el-menu-item index="1-1-2"><i class="el-icon-document"></i>選項1-2</el-menu-item></el-submenu><el-submenu index="1-2"><template slot="title"><i class="el-icon-location"></i><span>選項2</span></template><el-menu-item index="1-2-1"><i class="el-icon-setting"></i>選項2-1</el-menu-item><el-menu-item index="1-2-2"><i class="el-icon-setting"></i>選項2-2</el-menu-item></el-submenu></el-submenu><el-menu-item index="2"><i class="el-icon-document"></i>一級菜單2</el-menu-item><el-menu-item index="3"><i class="el-icon-setting"></i>一級菜單3</el-menu-item></el-menu></div>
</template><script>
export default {name: "Aside",
};
</script><style scoped>
.el-submenu {height: auto;
}
.el-icon-location,
.el-icon-document,
.el-icon-setting {display: inline-flex;align-items: center;justify-content: center;
}
</style>

1、加入相關事件

打開open、關閉close、選擇select 3個事件
在el-menu中加入三個事件屬性,并編寫對應的method

在這里插入圖片描述

全部參考代碼:

<template><div><el-menubackground-color="#545c64"text-color="#ffffff"active-text-color="#ffd04b"default-active="2"@open="handleOpen"@close="handleClose"@select="handSelect"><el-submenu index="1"><template slot="title"><i class="el-icon-location"></i><span>一級菜單1</span></template><el-submenu index="1-1"><template slot="title"><i class="el-icon-location"></i><span>選項1</span></template><el-menu-item index="1-1-1"><i class="el-icon-document"></i>選項1-1</el-menu-item><el-menu-item index="1-1-2"><i class="el-icon-document"></i>選項1-2</el-menu-item></el-submenu><el-submenu index="1-2"><template slot="title"><i class="el-icon-location"></i><span>選項2</span></template><el-menu-item index="1-2-1"><i class="el-icon-setting"></i>選項2-1</el-menu-item><el-menu-item index="1-2-2"><i class="el-icon-setting"></i>選項2-2</el-menu-item></el-submenu></el-submenu><el-menu-item index="2"><i class="el-icon-document"></i>一級菜單2</el-menu-item><el-menu-item index="3"><i class="el-icon-setting"></i>一級菜單3</el-menu-item></el-menu></div>
</template><script>
export default {name: "Aside",methods: {handleOpen(key, keyPath) {console.log("打開:", key, keyPath);},handleClose(key, keyPath) {console.log("關閉:", key, keyPath);},handSelect(key, keyPath) {console.log("選擇:", key, keyPath);},},
};
</script><style scoped>
.el-submenu {height: auto;
}
.el-icon-location,
.el-icon-document,
.el-icon-setting {display: inline-flex;align-items: center;justify-content: center;
}
</style>

四、菜單點擊跳轉

當點擊菜單項,能夠在右邊的Main窗口中顯示對應的頁面。

1. 創建新頁面

Main/ 文件夾下創建三個頁面,如圖所示
在這里插入圖片描述
代碼如下:

<template><div>這是Main1</div>
</template><script>export default {name: "Main1"}
</script><style scoped>
</style>

2. 配置路由

  • 安裝配置路由
  • 在src下創建 router/index.js
  • 創建了主路由index,就是進入的主頁面
  • 這3個index子路由,用來跳轉,分別對應 main1、main2、main3 幾個頁面。
  • 子路由的跳轉位置為,index的Main位置,因為我們管理系統只需要Main位置發生改變,頭部、左邊導航、下方footer是不需要改變的。

安裝路由

npm install vue-router@3.5.2

注意:

  • vue2搭配vue-router3
  • vue3搭配vue-router4

在main.js中注冊router,讓路由生效
在這里插入圖片描述

router/index.js代碼如下:

import VueRouter from "vue-router"
import Index from "@/components/Index";const routes = [//一級路由{path: '/index',name: 'index',component: Index,//路由嵌套children:[{path: '/index/menu1',component: () => import('@/components/Main/Main1.vue')},{path: '/index/menu2',component: () => import('@/components/Main/Main2.vue')},{path: '/index/menu3',component: () => import('@/components/Main/Main3.vue')}]}
]const router = new VueRouter({mode:'history',routes
})export  default router;

在原來的Index.vue頁面,設置路由跳轉位置,這里我們在原來的<Main />位置修改位 router-view即可。
在這里插入圖片描述

3. 菜單中加入路由配置

  • 這里我們使用一級菜單,簡單方便,修改Aside/index.vue的代碼。
  • el-menu里面加入 router屬性
  • el-menu-item 的index,設置對應的子路由

代碼參考如下:

<template><div style="height: 100%"><el-menubackground-color="#545c64"text-color="#ffffff"active-text-color="#ffd04b"class="el-menu-vertical-demo"router><el-menu-item index="/index/menu1"><i class="el-icon-location"></i>一級菜單1</el-menu-item><el-menu-item index="/index/menu2"><i class="el-icon-document"></i>一級菜單2</el-menu-item><el-menu-item index="/index/menu3"><i class="el-icon-setting"></i>一級菜單3</el-menu-item></el-menu></div>
</template><script>
export default {name: "Aside",
};
</script><style scoped>
.el-icon-location,
.el-icon-document,
.el-icon-setting {display: inline-flex;align-items: center;justify-content: center;
}
</style>

我們進入index主路由,發現頁面是空的
在這里插入圖片描述
點擊左側導航菜單,就會有對應的頁面內容
在這里插入圖片描述

4、處理默認的Main窗口為空的情況

設置默認的跳轉位置,設置如下:

  • 在子路由中添加一個新的路由,用來默認跳轉
  • 在主路由中配置redirect 值為這個子路由

router/index.js的代碼如下

import VueRouter from "vue-router"
import Index from "@/components/Index";const routes = [//一級路由{path: '/index',name: 'index',component: Index,redirect: 'index/Main',//路由嵌套children:[{path: '/index/Main',component: () => import('@/components/Main/index.vue')},{path: '/index/menu1',component: () => import('@/components/Main/Main1.vue')},{path: '/index/menu2',component: () => import('@/components/Main/Main2.vue')},{path: '/index/menu3',component: () => import('@/components/Main/Main3.vue')}]}
]const router = new VueRouter({mode:'history',routes
})export  default router;

其實就是一個重定向的操作,當直接輸入index路由時就會默認跳轉到Main路由里面,這樣就會有個默認的頁面了。
下方我們在地址欄只輸入到index,敲回車后,會在后面默認加上 “/Main”,直接重定向了,同時Main窗口的頁面也顯示了我們指定的頁面。

在這里插入圖片描述

五、動態左側菜單導航

在項目中,比較多見的是會將菜單存儲到后臺的數據庫中,通過返回數據來決定菜單的模樣,并不是由前端來控制菜單的模樣,所以就來實現動態的菜單導航,根據后臺數據來生成菜單導航。

在這里插入圖片描述

1、動態實現一級菜單

分析上述代碼,其中代碼 <el-menu-item index=“/index/menu1”>一級菜單1</el-menu-item> 是比較相似的,不同的地方有3個:

  • index 表示路由的path
  • 圖標的名稱
  • 菜單的名稱

基于以上幾個不同,我們可以考慮設置一個數組,數組的元素包含 路由路徑、圖標名、菜單名等幾個屬性,然后以循環的方式來輸出這個菜單。

實現代碼:

<template><div style="height: 100%"><el-menubackground-color="#545c64"text-color="#ffffff"active-text-color="#ffd04b"class="el-menu-vertical-demo"router><el-menu-item:index="item.path"v-for="item in menu_data":key="item.name"><i :class="item.icon"></i>{{ item.name }}</el-menu-item></el-menu></div>
</template><script>
export default {name: "Aside",data() {return {menu_data: [{name: "一級菜單1",icon: "el-icon-location",path: "/index/menu1",},{name: "一級菜單2",icon: "el-icon-document",path: "/index/menu2",},{name: "一級菜單3",icon: "el-icon-setting",path: "/index/menu3",},],};},
};
</script><style scoped>
.el-icon-location,
.el-icon-document,
.el-icon-setting {display: inline-flex;align-items: center;justify-content: center;
}
</style>
  1. 菜單數據menu_data包含3個元素,每個元素分別有name、icon和path屬性,這個三個屬性分別對應菜單名、圖標、路由的路徑。
  2. 使用v-for循環menu_data,填入對應的屬性就可。

2、動態實現二級菜單

在一級菜單的數據對象里面加入 child 屬性,這個屬性也是跟現在的菜單數據menu_data一樣的,
child也是一個數組,包含多個元素,每個元素分別有name、icon和path屬性,這個三個屬性分別對應菜單名、圖標、路由的路徑。

在這里插入圖片描述
參考代碼:

<template><div style="height: 100%"><el-menubackground-color="#545c64"text-color="#ffffff"active-text-color="#ffd04b"class="el-menu-vertical-demo"router><el-submenu :index="item.path" v-for="item in menu_data" :key="item.name"><template slot="title"><i :class="item.icon"></i><span>{{ item.name }}</span></template><el-menu-item:index="child.path"v-for="child in item.child":key="child.name"><i :class="child.icon"></i>{{ child.name }}</el-menu-item></el-submenu></el-menu></div>
</template><script>
export default {name: "Aside",data() {return {menu_data: [{name: "一級菜單1",icon: "el-icon-location",path: "/index/menu1",child: [{name: "二級菜單1-1",icon: "el-icon-user",path: "/index/menu11",},{name: "二級菜單1-2",icon: "el-icon-user-solid",path: "/index/menu12",},],},{name: "一級菜單2",icon: "el-icon-document",path: "/index/menu2",child: [{name: "二級菜單2-1",icon: "el-icon-star-on",path: "/index/menu21",},{name: "二級菜單2-2",icon: "el-icon-star-off",path: "/index/menu22",},],},{name: "一級菜單3",icon: "el-icon-setting",path: "/index/menu3",child: [{name: "二級菜單3-1",icon: "el-icon-s-help",path: "/index/menu31",},{name: "二級菜單3-2",icon: "el-icon-help",path: "/index/menu32",},],},],};},
};
</script><style scoped>
.el-submenu{height: auto;
}.el-icon-help,
.el-icon-s-help,
.el-icon-star-off,
.el-icon-star-on,
.el-icon-user-solid,
.el-icon-user,
.el-icon-location,
.el-icon-document,
.el-icon-setting {display: inline-flex;align-items: center;justify-content: center;
}
</style>

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/716167.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/716167.shtml
英文地址,請注明出處:http://en.pswp.cn/news/716167.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

SRIO--IP講解及環回測試

提示:文章寫完后,目錄可以自動生成,如何生成可參考右邊的幫助文檔 文章目錄 前言一、IP例化文件二、SRIO環回工程搭建三、板級驗證3.1 板級驗證環節3.2 系統所需硬件3.3 ILA波形前言 本章將為大家介紹 “Serial RapidIO Gen2 ”IP 的使用以及配置方法。“Serial RapidIO Ge…

JavaScript入門學(Web APIs)

1.變量聲明 2 DOM介紹 2.1 什么是DOM 2.2 DOM樹 2.3 DOM對象&#xff08;重要&#xff09; 3.DOM&#xff08;文檔對象模型&#xff09;-獲取元素 3.1 獲取匹配的第一個元素 <!DOCTYPE html> <html lang"en"> <head><meta charset"UTF-8&…

Canvs的js庫:Fabric.js簡單強大,用于繪制各種圖形

Fabric.js是一個用于創建交互式的HTML5 Canvas應用程序的JavaScript庫。它提供了一個簡單而強大的API&#xff0c;用于在Web瀏覽器中繪制和操作圖形對象。Fabric.js可以用于創建各種圖形應用程序&#xff0c;例如繪圖編輯器、圖像編輯器、流程圖、地圖和數據可視化等。 官網文…

校企合作項目總結

校企合作總結 前言項目框架開發待辦水平越權總結 前言 寒假里小組給了校企合作的項目&#xff0c;分配給我的工作量總共也就兩三套crud&#xff0c;雖然工作很少&#xff0c;但還是從里面學到了很多東西&#xff0c;收獲了大量的實習經驗&#xff0c;在這里總結記錄一下。 項…

FreeRTOS學習筆記——FreeRTOS中斷管理

精華總結&#xff1a; 中斷優先級0為最高&#xff0c;任務優先級0為最低 中斷優先級分組中為方便rtos管理4bit全部設置成搶占優先級 32單片機的中斷管理是由3個寄存器完成&#xff08;名字忽略&#xff0c;具體功能忽略&#xff09;&#xff0c;三個寄存器都是32bit&#xff0c…

微信小程序云開發教程——墨刀原型工具入門(文件設置+編輯組件)

引言 作為一個小白&#xff0c;小北要怎么在短時間內快速學會微信小程序原型設計&#xff1f; “時間緊&#xff0c;任務重”&#xff0c;這意味著學習時必須把握微信小程序原型設計中的重點、難點&#xff0c;而非面面俱到。 要在短時間內理解、掌握一個工具的使用&#xf…

NOC2023軟件創意編程(學而思賽道)python小高組決賽真題

目錄 下載原文檔打印做題: 軟件創意編程 一、參賽范圍 1.參賽組別:小學低年級組(1-3 年級)、小學高年級組(4-6 年級)、初中組。 2.參賽人數:1 人。 3.指導教師:1 人(可空缺)。 4.每人限參加 1 個賽項。 組別確定:以地方教育行政主管部門(教委、教育廳、教育局) 認…

【風格遷移】StyTr2:引入 Transformer 解決 CNN 在長距離依賴性處理不足和細節丟失問題

StyTr2&#xff1a;引入 Transformer 解決 CNN 在長距離依賴性處理不足和細節丟失問題 提出背景StyTr2 組成StyTr2 架構 提出背景 論文&#xff1a;https://arxiv.org/pdf/2105.14576.pdf 代碼&#xff1a;https://github.com/diyiiyiii/StyTR-2 問題&#xff1a; 傳統的神經…

idea中springboot項目創建后追加依賴

springboot項目創建后追加依賴 前言1、安裝插件editstarters設置->插件 2、進入pom.xml 頁面 前言 在項目創建的時候選擇好依賴創建項目&#xff0c;之后追加依賴不是很方便&#xff0c;介紹一個簡單的使用方法&#xff0c;通過editstarters進行添加 1、安裝插件editstart…

在 Ubuntu 終端輸出不同顏色、粗體、下劃線或其他樣式的字體

嗯。調試時總發現自己打印的調試信息太過普通、單調&#xff0c;于是乎…… Notice 要在終端實現字體的特殊樣式&#xff0c;通常通過使用特殊的控制字符來實現&#xff0c;而不是通過某語言本身的功能來實現。 在大多數終端中&#xff0c;可以使用 ANSI 轉義序列來設置字體的…

CleanMyMac X2024測評深度分析與功能全面介紹

一、軟件概述 CleanMyMac X 是一款強大的Mac清理和優化工具&#xff0c;它可以幫助用戶輕松管理和釋放Mac上的空間&#xff0c;優化系統性能&#xff0c;提高運行速度。這款軟件以其直觀的用戶界面和豐富的功能受到了廣大Mac用戶的歡迎。 CleanMyMac X4.14.6全新版下載如下: …

令牌桶算法和漏桶算法各自的應用場景

令牌桶算法和漏桶算法都是流量控制算法&#xff0c;它們在網絡和系統中有著不同的應用場景&#xff0c;具體如下&#xff1a; 令牌桶算法的應用場景&#xff1a; 網絡流量控制&#xff1a; 令牌桶算法廣泛應用于網絡流量控制中&#xff0c;特別是在網絡設備中&#xff0c;如路…

html基礎標簽+Http請求

文章目錄 目錄 文章目錄 前言 一.網址組成 二.HTTP協議解析 Http 請求報文 報文請求方法 報文頭 Cache-Control 常見緩存控制行為 cookie 解析 Http 響應報文 常見狀態碼 三.域名解析(DNS) DNS域名服務器分類 遞歸查詢 迭代查詢 四.端口號 五.路徑信息 六.Https協議 ?對稱…

第一篇【傳奇開心果系列】Python的自動化辦公庫技術點案例示例:深度解讀Pandas庫

傳奇開心果博文系列 系列博文目錄Python的自動化辦公庫技術點案例示例系列 博文目錄前言一、主要特點和功能介紹二、Series 示例代碼三、DataFrame示例代碼四、數據導入/導出示例代碼五、數據清洗示例代碼六、數據選擇和過濾示例代碼七、數據合并和連接示例代碼八、數據分組和聚…

Linux系統管理:虛擬機 Kali Linux 安裝

目錄 一、理論 1.Kali Linux 二、實驗 1.虛擬機Kali Linux安裝準備階段 2.安裝Kali Linux 2. Kali Linux 更換國內源 3. Kali Linux 設置固定IP 4. Kali Linux 開啟SSH遠程連接 5. MobaXterm遠程連接 Kali Linux 三、問題 1.apt 命令 取代哪些 apt-get命令 一、理論…

《OpenScene: 3D Scene Understanding with Open Vocabularies》閱讀筆記1

傳統的3D場景理解方法依賴于帶標簽的3D數據集,用于訓練一個模型以進行單一任務的監督學習。我們提出了OpenScene,一種替代方法,其中模型在CLIP特征空間中預測與文本和圖像像素共同嵌入的3D場景點的密集特征。這種零樣本方法實現了與任務無關的訓練和開放詞匯查詢。例如,為了…

Phoncent博客:探索AI寫作與編程的無限可能

Phoncent博客&#xff0c;一個名為Phoncent的創新AIGC博客網站&#xff0c;于2023年誕生。它的創始人是莊澤峰&#xff0c;一個自媒體人和個人站長&#xff0c;他在網絡營銷推廣領域有著豐富的經驗。莊澤峰深知人工智能技術在內容創作和編程領域的潛力和創造力&#xff0c;因此…

有趣的CSS - 閃爍的鴻星爾克文字招牌效果

大家好&#xff0c;我是 Just&#xff0c;這里是「設計師工作日常」&#xff0c;今天分享的是利用 animation 動畫實現一個閃爍的霓虹燈文字效果。 《有趣的css》系列最新實例通過公眾號「設計師工作日常」發布。 目錄 整體效果核心代碼html 代碼css 部分代碼 完整代碼如下html…

第十一屆藍橋杯省賽第二場C++ B組 / C組《成績統計》(c++)

1.題目說明 小藍給學生們組織了一場考試&#xff0c;卷面總分為100 分&#xff0c;每個學生的得分都是一個 0 到 100 的整數。 如果得分至少是 60 分&#xff0c;則稱為及格。 如果得分至少為 85 分&#xff0c;則稱為優秀。 請計算及格率和優秀率&#xff0c;用百分數表示…

使用Spark探索數據

需求分析 使用Spark來探索數據是一種高效處理大規模數據的方法&#xff0c;需要對數據進行加載、清洗和轉換&#xff0c;選擇合適的Spark組件進行數據處理和分析。需求分析包括確定數據分析的目的和問題、選擇合適的Spark應用程序和算法、優化數據處理流程和性能、可視化和解釋…