這篇文章主要介紹了關於Vue iview-admin框架二級菜單改為三級菜單的方法,有著一定的參考價值,現在分享給大家,有需要的朋友可以參考一下
最近在用iview-admin的Vue後台模板,從git上下載後發現左側導航欄最多支持到二級菜單,也發現很多童鞋在問如何實現三級菜單。在實際的應用場景中還是會出現三級菜單的需求的,木有其他好方法,只能自己手動改代碼了。
1. 第一步:先改寫VUE中的模板,修改sidebarMenu.vue文件,文件具體目錄建下圖:
將Menu導航選單組件的的二級嵌套結構改為三級嵌套,無非就是判斷二級路由頁面下是否有children屬性及是否含有子元素,有的話直接v-for循環生成子元素標籤,新結構如下:
<template> <menu> <template> <menuitem> <icon></icon> <span>{{ itemTitle(item.children[0]) }}</span> </menuitem> <submenu> 1" :name="item.name" :key="item.name"> <template> <icon></icon> <span>{{ itemTitle(item) }}</span> </template> <template> <!-- 添加条件判断是否还有三级菜单 v-if="child.children.length<=1" --> <menuitem> <icon></icon> <span>{{ itemTitle(child) }}</span> </menuitem> <!-- 以下为新增 添加条件判断如果有三级菜单 则增加三级菜单 --> <submenu> <template> <icon></icon> <span>{{ itemTitle(child) }}</span> </template> <template> <menuitem> <icon></icon> <span>{{ itemTitle(son) }}</span> </menuitem> </template> </submenu> <!-- 以上为新增 --> </template> </submenu> </template> </menu> </template>
元件中methods下新增一個方法isThirdLeveMenu,判斷是否含有children屬性:
methods: { changeMenu(active) { this.$emit("on-change", active); }, itemTitle(item) { if (typeof item.title === "object") { return this.$t(item.title.i18n); } else { return item.title; } }, isThirdLeveMenu(child){ if(child.children){ if(child.children.length>0)return true; else return false; } else { return false; } } },
第二步:修改建立目前path路徑的邏輯方法:setCurrentPath ,在libs資料夾下util.js檔案中:
util.setCurrentPath = function (vm, name) { let title = ''; let isOtherRouter = false; vm.$store.state.app.routers.forEach(item => { if (item.children.length === 1) { if (item.children[0].name === name) { title = util.handleTitle(vm, item); if (item.name === 'otherRouter') { isOtherRouter = true; } } } else { item.children.forEach(child => { if (child.name === name) { title = util.handleTitle(vm, child); if (item.name === 'otherRouter') { isOtherRouter = true; } } }); } }); let currentPathArr = []; //去首页 if (name === 'home_index') { currentPathArr = [ { title: util.handleTitle(vm, util.getRouterObjByName(vm.$store.state.app.routers, 'home_index')), path: '', name: 'home_index' } ]; } //去导航菜单一级页面或者OtherRouter路由中的页面 else if ((name.indexOf('_index') >= 0 || isOtherRouter) && name !== 'home_index') { currentPathArr = [ { title: util.handleTitle(vm, util.getRouterObjByName(vm.$store.state.app.routers, 'home_index')), path: '/home', name: 'home_index' }, { title: title, path: '', name: name } ]; } //去导航菜单二级页面或三级页面 else { let currentPathObj = vm.$store.state.app.routers.filter(item => { var hasMenu; if (item.children.length { return child.name === name; })[0]; // let thirdLevelObj = console.log(childObj) //二级页面 if (childObj) { currentPathArr = [ { title: '首页', path: '/home', name: 'home_index' }, { title: currentPathObj.title, path: '', name: currentPathObj.name }, { title: childObj.title, path: currentPathObj.path + '/' + childObj.path, name: name } ]; } //childobj为undefined,再从三级页面中遍历 else { let thirdObj; let childObj = currentPathObj.children.filter((child) => { let hasChildren; hasChildren = child.name === name; if (hasChildren) return hasChildren if (child.children) { let sonArr = child.children; for (let n = 0; n <p><strong>第三個步驟:建立三級頁面test-child.vue,testcaca.vue和三級路由的容器組件artical-publish-center.vue</strong><br><strong>artical-publish-center.vue結構如下圖: 要有<rout-view>標籤</rout-view></strong></p><p><img src="/static/imghwm/default1.png" data-src="https://img.php.cn//upload/image/524/766/974/1531115697133053.png?x-oss-process=image/resize,p_40" class="lazy" title="1531115697133053.png" alt="Vue iview-admin框架二級選單改為三級選單的方法"></p><p>##其他兩個三級頁面vue隨便寫了:</p><p><img src="/static/imghwm/default1.png" data-src="https://img.php.cn//upload/image/142/703/339/1531115707261749.png?x-oss-process=image/resize,p_40" class="lazy" title="1531115707261749.png" alt="Vue iview-admin框架二級選單改為三級選單的方法"></p><p><strong>#第四步:到這裡,容器可以實現期待已久三級選單了,^_^. 在router裡新增三級頁面路由,router資料夾下router.js中:</strong><br>加到appRouter中吧,可以放到title: '元件'的children數組中: </p><pre class="brush:php;toolbar:false">{ path: 'artical-publish', title: '用户配置', name: 'artical-publish', icon: 'compose', component: () => import('@/views/test/artical-publish-center.vue'), //引用三级页面的中间容器层 children:[ { path: 'testcaca', icon: 'ios-pause', name: 'testcaca', title: 'test4', component: () => import('@/views/test/testcaca.vue') }, { path: 'test-child', icon: 'ios-pause', name: 'test-child', title: 'test-child', component: () => import('@/views/test/test-child.vue') } ] }
最後儲存,運行你的項目,看下三級選單出來了:
以上是Vue iview-admin框架二級選單改為三級選單的方法的詳細內容。更多資訊請關注PHP中文網其他相關文章!

JavaScript可用於前端和後端開發。前端通過DOM操作增強用戶體驗,後端通過Node.js處理服務器任務。 1.前端示例:改變網頁文本內容。 2.後端示例:創建Node.js服務器。

選擇Python還是JavaScript應基於職業發展、學習曲線和生態系統:1)職業發展:Python適合數據科學和後端開發,JavaScript適合前端和全棧開發。 2)學習曲線:Python語法簡潔,適合初學者;JavaScript語法靈活。 3)生態系統:Python有豐富的科學計算庫,JavaScript有強大的前端框架。

JavaScript框架的強大之處在於簡化開發、提升用戶體驗和應用性能。選擇框架時應考慮:1.項目規模和復雜度,2.團隊經驗,3.生態系統和社區支持。

引言我知道你可能會覺得奇怪,JavaScript、C 和瀏覽器之間到底有什麼關係?它們之間看似毫無關聯,但實際上,它們在現代網絡開發中扮演著非常重要的角色。今天我們就來深入探討一下這三者之間的緊密聯繫。通過這篇文章,你將了解到JavaScript如何在瀏覽器中運行,C 在瀏覽器引擎中的作用,以及它們如何共同推動網頁的渲染和交互。 JavaScript與瀏覽器的關係我們都知道,JavaScript是前端開發的核心語言,它直接在瀏覽器中運行,讓網頁變得生動有趣。你是否曾經想過,為什麼JavaScr

Node.js擅長於高效I/O,這在很大程度上要歸功於流。 流媒體匯總處理數據,避免內存過載 - 大型文件,網絡任務和實時應用程序的理想。將流與打字稿的類型安全結合起來創建POWE

Python和JavaScript在性能和效率方面的差異主要體現在:1)Python作為解釋型語言,運行速度較慢,但開發效率高,適合快速原型開發;2)JavaScript在瀏覽器中受限於單線程,但在Node.js中可利用多線程和異步I/O提升性能,兩者在實際項目中各有優勢。

JavaScript起源於1995年,由布蘭登·艾克創造,實現語言為C語言。 1.C語言為JavaScript提供了高性能和系統級編程能力。 2.JavaScript的內存管理和性能優化依賴於C語言。 3.C語言的跨平台特性幫助JavaScript在不同操作系統上高效運行。

JavaScript在瀏覽器和Node.js環境中運行,依賴JavaScript引擎解析和執行代碼。 1)解析階段生成抽象語法樹(AST);2)編譯階段將AST轉換為字節碼或機器碼;3)執行階段執行編譯後的代碼。


熱AI工具

Undresser.AI Undress
人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover
用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

Video Face Swap
使用我們完全免費的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱門文章

熱工具

MantisBT
Mantis是一個易於部署的基於Web的缺陷追蹤工具,用於幫助產品缺陷追蹤。它需要PHP、MySQL和一個Web伺服器。請查看我們的演示和託管服務。

Dreamweaver CS6
視覺化網頁開發工具

記事本++7.3.1
好用且免費的程式碼編輯器

禪工作室 13.0.1
強大的PHP整合開發環境

SAP NetWeaver Server Adapter for Eclipse
將Eclipse與SAP NetWeaver應用伺服器整合。