element實(shí)現(xiàn)二級(jí)菜單和頂部導(dǎo)航聯(lián)動(dòng)的示例
目前
原本數(shù)據(jù)結(jié)構(gòu)
const users = [{ id: 1, username: 'normal', password: 'normal', token: 'abcdefghijklmnopqrstuvwxyz', leftMenus: [{ title: '用戶管理', key: '/user', name: 'user', icon: 'el-icon-user-solid', rights: ['view', 'edit', 'add', 'delete'] }, { title: '商品管理', key: '/products', name: 'products', icon: 'el-icon-s-goods', children: [{ title: '品類管理', key: '/category', name: 'category', icon: 'el-icon-s-ticket', rights: ['view', 'edit', 'add', 'delete'] }, { title: '商品生產(chǎn)', key: '/product', name: 'product', icon: 'el-icon-s-promotion', rights: ['view', 'edit', 'add', 'delete'] } ] }, { title: '圖形圖表', key: '/charts', name: 'charts', icon: 'el-icon-picture', children: [{ title: '柱線圖', key: '/charts/bar', name: 'bar', icon: 'el-icon-s-data', rights: ['view', 'edit', 'add', 'delete'] }, { title: '折線圖', key: '/charts/line', name: 'line', icon: 'el-icon-s-marketing', rights: ['view', 'edit', 'add', 'delete'] }, { title: '餅圖', key: '/charts/pie', name: 'pie', icon: 'el-icon-s-help', rights: ['view', 'edit', 'add', 'delete'] }, ] } ] }, { id: 2, username: 'admin', password: 'admin', token: 'abcdefghijklmnopqrstuvwxyz'.split('').reverse().join(''), leftMenus: [{ title: '用戶管理', key: '/user', name: 'user', icon: 'el-icon-user-solid', rights: ['view', 'edit', 'add', 'delete'] }, { title: '商品管理', key: '/products', name: 'products', icon: 'el-icon-s-goods', children: [{ title: '品類管理', key: '/category', name: 'category', icon: 'el-icon-s-ticket', rights: ['view', 'edit', 'add', 'delete'] }, { title: '商品生產(chǎn)', key: '/product', name: 'product', icon: 'el-icon-s-promotion', rights: ['view', 'edit', 'add', 'delete'] } ] }, { title: '角色管理', key: '/role', name: 'role', icon: 'el-icon-s-custom', rights: ['view', 'edit', 'add', 'delete'] }, { title: '圖形圖表', key: '/charts', name: 'charts', icon: 'el-icon-picture', children: [{ title: '柱線圖', key: '/charts/bar', name: 'bar', icon: 'el-icon-s-data', rights: ['view', 'edit', 'add', 'delete'] }, { title: '折線圖', key: '/charts/line', name: 'line', icon: 'el-icon-s-marketing', rights: ['view', 'edit', 'add', 'delete'] }, { title: '餅圖', key: '/charts/pie', name: 'pie', icon: 'el-icon-s-help', rights: ['view', 'edit', 'add', 'delete'] }, ] } ] } ]
因?yàn)樾略鲰敳繉?dǎo)航,所以要重新構(gòu)建數(shù)據(jù)結(jié)構(gòu) 。
修改后的數(shù)據(jù)結(jié)構(gòu)
const users = [{ id: 1, username: 'normal', password: 'normal', token: 'abcdefghijklmnopqrstuvwxyz', navBar: { active: '0', list: [{ name: "首頁", subActive: '0', leftMenus: [{ id: 1, title: '用戶管理1', key: '/user', name: 'user', icon: 'el-icon-user-solid', rights: ['view', 'edit', 'add', 'delete'] }, { id: 2, title: '商品管理', name: 'products', icon: 'el-icon-s-goods', children: [{ id: 21, title: '品類管理', key: '/category', name: 'category', icon: 'bars', rights: ['view', 'edit', 'add', 'delete'] }, { id: 22, title: '商品生產(chǎn)', key: '/product', name: 'product', icon: 'tool', rights: ['view', 'edit', 'add', 'delete'] } ] }, { id: 4, title: '圖形圖表', name: 'charts', icon: 'el-icon-s-platform', children: [{ id: 41, title: '柱線圖', key: '/charts/bar', name: 'bar', icon: 'el-icon-s-data', rights: ['view', 'edit', 'add', 'delete'] }, { id: 42, title: '折線圖', key: '/charts/line', name: 'line', icon: 'el-icon-s-marketing', rights: ['view', 'edit', 'add', 'delete'] }, { id: 43, title: '餅圖', key: '/charts/pie', name: 'pie', icon: 'el-icon-s-help', rights: ['view', 'edit', 'add', 'delete'] }, ] } ] }, { name: "商品", subActive: '0', leftMenus: [{ id: 1, title: '用戶管理2', key: '/user2', name: 'user2', icon: 'el-icon-user-solid', rights: ['view', 'edit', 'add', 'delete'] }] }, { name: "訂單", subActive: '0', leftMenus: [{ id: 32, title: '訂單管理', key: '/dingg', name: 'dingg', icon: 'el-icon-user-solid', rights: ['view', 'edit', 'add', 'delete'] }, { id: 34, title: '訂單統(tǒng)計(jì)', name: 'ding', icon: 'el-icon-user-solid', children: [{ id: 35, title: '訂單匯總', key: '/ding/hui', name: 'dinghui', icon: 'el-icon-s-data', rights: ['view', 'edit', 'add', 'delete'] }, ] } ] }, { name: "會(huì)員" }, { name: "設(shè)置" }, ] } }, { id: 2, username: 'admin', password: 'admin', token: 'abcdefghijklmnopqrstuvwxyz'.split('').reverse().join(''), /* rights: [{ id: 1, authName: '用戶管理', icon: 'icon-menu', children: [{ id: 11, authName: '用戶項(xiàng)目1', path: '/menu/one', rights: ['view', 'edit', 'add'] }, { id: 12, authName: '用戶項(xiàng)目2', path: '/menu/two', rights: ['view', 'edit', 'add', 'delete'] }] }, { id: 2, authName: '用戶權(quán)限', icon: 'icon-menu', children: [{ id: 22, authName: '權(quán)限項(xiàng)目1', path: '/menu/three', rights: ['view', 'edit', 'add', 'delete'] }] }, { id: 3, authName: '用戶信息', icon: 'icon-menu' }, ] */ leftMenus: [{ id: 1, title: '用戶管理', key: '/user', name: 'user', icon: 'el-icon-user-solid', rights: ['view', 'edit', 'add', 'delete'] }, { id: 2, title: '商品管理', key: '/products', name: 'products', icon: 'el-icon-s-goods', children: [{ id: 21, title: '品類管理', key: '/category', name: 'category', icon: 'bars', rights: ['view', 'edit', 'add', 'delete'] }, { id: 22, title: '商品生產(chǎn)', key: '/product', name: 'product', icon: 'tool', rights: ['view', 'edit', 'add', 'delete'] } ] }, { id: 3, title: '角色管理', key: '/role', name: 'role', icon: 'el-icon-s-custom', rights: ['view', 'edit', 'add', 'delete'] }, { id: 4, title: '圖形圖表', key: '/charts', name: 'charts', icon: 'el-icon-s-platform', children: [{ id: 41, title: '柱線圖', key: '/charts/bar', name: 'bar', icon: 'el-icon-s-data', rights: ['view', 'edit', 'add', 'delete'] }, { id: 42, title: '折線圖', key: '/charts/line', name: 'line', icon: 'el-icon-s-marketing', rights: ['view', 'edit', 'add', 'delete'] }, { id: 43, title: '餅圖', key: '/charts/pie', name: 'pie', icon: 'el-icon-s-help', rights: ['view', 'edit', 'add', 'delete'] }, ] } ] } ]
頭部布局
<div class="header-right"> <el-menu :default-active="user.navBar.active" class="el-menu-demo" mode="horizontal" @select="handleSelect" background-color="#545c64" text-color="#fff" active-text-color="#ffd04b"> <el-menu-item :index="index+''" v-for="(item,index) in user.navBar.list" :key="index">{{item.name}}</el-menu-item> <el-submenu index="2"> <template slot="title">{{user.username}}</template> <el-menu-item index="2-1">設(shè)置</el-menu-item> <el-menu-item index="2-2">退出</el-menu-item> </el-submenu> </el-menu> </div>
methods: { ...mapMutations(["getslideMenus"]), handleSelect(key, keyPath) { this.user.navBar.active = key; let item = this.user.navBar.list[this.user.navBar.active]; console.log(key, keyPath); this.getslideMenus(item.leftMenus); this.$router.push({ name:item.leftMenus[0].name }) if (key == '0') { this.$router.push({ name:'welcome' }) } }, collapse() { this.isCollapseq = !this.isCollapseq; this.$bus.$emit("collapseaside", this.isCollapseq); }, }, mounted() { let item = this.user.navBar.list[this.user.navBar.active] this.getslideMenus(item.leftMenus) }, computed: { ...mapState(['user']) }
vuex
export default new Vuex.Store({ state: { bread: [], slideMenus:[], user: JSON.parse(window.sessionStorage.getItem('rightlist')) || {} }, mutations: { addBread(state,preload) { state.bread = preload }, getslideMenus(state,preload){ state.slideMenus = preload }, getLogin(state, preload) { state.user = preload window.sessionStorage.setItem('rightlist', JSON.stringify(preload)) } }, getters: { }, actions: {} })
Home.vue
methods: { ...mapMutations(["addBread"]), /* 面包屑導(dǎo)航 */ getbreadcrumb() { let getobj = this.$route.matched.filter((v) => v.name); console.log(getobj); let arr = []; getobj.forEach((v) => { if (v.name == "welcome" || v.name == "Home") { return; } arr.push({ name: v.name, path: v.path, title: v.meta.title, }); /* 多級(jí)導(dǎo)航 */ this.slideMenus.forEach((item) => { console.log(item); if (item.children) { item.children.filter((i) => i.key == v.path).length > 0 ? arr.unshift({ title: item.title }) : arr; } }); }); if (arr.length > 0) { arr.unshift({ name: "Home", path: "/", title: "后臺(tái)首頁", }); } this.bran = arr; this.addBread(this.bran); }, },
HomeLeft添加個(gè)樣式判斷
<template> <div class="navMenu"> <label v-for="(navMenu,n) in navMenus" :key="n"> <!--只有一級(jí)菜單--> <el-menu-item v-if="!navMenu.children" :index="n + ''" :route="navMenu.key" :style="$route.path == navMenu.key?'color:#ffd04b':''" > <!--圖標(biāo)--> <i :class="navMenu.icon" :style="$route.path == navMenu.key?'color:#ffd04b':''"></i> <!--標(biāo)題--> <span slot="title">{{navMenu.title}}</span> </el-menu-item> <!--有多級(jí)菜單--> <el-submenu v-if="navMenu.children" :key="navMenu.key" :index="n+''" > <template slot="title"> <i :class="navMenu.icon"></i> <span> {{navMenu.title}}</span> </template> <!--遞歸組件,把遍歷的值傳回子組件,完成遞歸調(diào)用--> <nav-menu :navMenus="navMenu.children"></nav-menu> </el-submenu> </label> </div> </template> <script> export default { name: 'NavMenu', //使用遞歸組件必須要有 props: ['navMenus'], // 傳入子組件的數(shù)據(jù) data() { return {} }, methods: { handleSelect(key,keyPath){ console.log('1212') console.log(key,keyPath) } } } </script> <style scoped> </style>
加個(gè)路由
import User2 from '../views/User2.vue' { path: '/user2', name:'user2', meta:{title:'用戶管理2'}, component: User2 },
現(xiàn)在:
到此這篇關(guān)于element實(shí)現(xiàn)二級(jí)菜單和頂部導(dǎo)航聯(lián)動(dòng)的示例的文章就介紹到這了,更多相關(guān)element 二級(jí)菜單和頂部導(dǎo)航聯(lián)動(dòng)內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
layui實(shí)際項(xiàng)目使用過程中遇到的兼容性問題及解決
這篇文章主要介紹了layui實(shí)際項(xiàng)目使用過程中遇到的兼容性問題及解決方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-04-04vue項(xiàng)目中微信登錄的實(shí)現(xiàn)操作
這篇文章主要介紹了vue項(xiàng)目中微信登錄的實(shí)現(xiàn)操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2020-09-09vue3點(diǎn)擊按鈕下載文件功能的代碼實(shí)現(xiàn)
在寫vue項(xiàng)目時(shí),有個(gè)需求是點(diǎn)擊表格中某一行的下載按鈕,然后開始下載這一行對(duì)應(yīng)的文件,所以本文小編給大家介紹了使用vue3實(shí)現(xiàn)點(diǎn)擊按鈕下載文件功能,文中有詳細(xì)的代碼示例供大家參考,需要的朋友可以參考下2024-01-01Vue3+TypeScript+Vite使用require動(dòng)態(tài)引入圖片等靜態(tài)資源
本文主要介紹了Vue3+TypeScript+Vite使用require動(dòng)態(tài)引入圖片等靜態(tài)資源,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2022-07-07Vue3.2單文件組件setup的語法糖與新特性總結(jié)
ue3上線已經(jīng)很久了,許多小伙伴應(yīng)該都已經(jīng)使用過vue3了,下面這篇文章主要給大家介紹了關(guān)于Vue3.2單文件組件setup的語法糖與新特性總結(jié)的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),需要的朋友可以參考下2022-07-07vue腳手架配置預(yù)渲染及prerender-spa-plugin配置方式
這篇文章主要介紹了vue腳手架配置預(yù)渲染及prerender-spa-plugin配置方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-05-05