vue實(shí)現(xiàn)el-menu和el-tab聯(lián)動(dòng)的示例代碼
vue通過(guò)el-menus和el-tabs聯(lián)動(dòng),實(shí)現(xiàn)點(diǎn)擊側(cè)邊欄,頁(yè)面內(nèi)顯示一行tab以及點(diǎn)擊tab切換路由
實(shí)現(xiàn)效果如下
實(shí)現(xiàn)思路 左側(cè)邊欄添加點(diǎn)擊事件/設(shè)置el-menu的路由模式,然后監(jiān)聽路由的變化,拿到的路由去改變el-tabs綁定的屬性,然后改變el-tab-pane循環(huán)的數(shù)組,然后設(shè)置el-tabs的點(diǎn)擊/刪除事件,最終實(shí)現(xiàn)聯(lián)動(dòng) 首先使用vuex定義公共狀態(tài)openTab以及activeIndexTab 也就是循環(huán)的數(shù)組和當(dāng)前高亮
import Vue from "vue" import Vuex from "vuex" Vue.use(Vuex) export default new Vuex.Store({ state: { openTab: [], activeIndexTab: '' }, mutations: { //添加tab事件 add_tabs (state, data) { state.openTab.push(data) }, //刪除 delete_tabs (state, name) { let index = 0 for (let item of state.openTab) { if (item.name === name) { break } index++ } state.openTab.splice(index, 1) }, //設(shè)置高亮tab set_active_index (state, index) { state.activeIndexTab = index }, }, })
html模板
<el-menu> <div v-for="(item, index) in menuList" :key="index"> <el-menu-item :index="item.index" :class="{'isActive':activeIndex == item.index}" @click="routeTo(item)"> <i :class="['icon', item.name]"></i> <span slot="title">{{ item.title }}</span> </el-menu-item> </div> </el-menu> <el-tabs v-model="activeIndexTab" type="card" @tab-click="clickTab" @tab-remove="removeTab" closable> <el-tab-pane v-for="item of openTab" v-if="openTab.length" :key="item.title" :label="item.title" :name="item.name"> </el-tab-pane> </el-tabs>
定義data函數(shù)中要用到的屬性
data() { return { activeIndex: "", menuList:[ {"index":"1","title":"商戶資料管理","name":"meansManage"}, {"index":"2","title":"商戶訂單管理","name":"payOrderManage"}, {"index":"3","title":"商戶報(bào)表管理","name":"reportManage"}, ] } },
在vuex中取到el-tabs用到的屬性
computed: { openTab () { return this.$store.state.openTab }, activeIndexTab: { get () { return this.$store.state.activeIndexTab }, set (val) { this.$store.commit('set_active_index', val) } }, },
路由配置信息如下
{ path: "/", component: frame, redirect: "/meansManage", children: [ { path: "/meansManage", name: "meansManage", meta:{title:'商戶資料管理'}, component: () => import("../components/merchantManage/meansManage/index.vue") }, { path: "/payOrderManage", name: "payOrderManage", meta:{title:'商戶訂單管理'}, component: () => import("../components/merchantManage/payOrderManage/orderIndex.vue") }, { path:'/reportManage', name:'reportManage', meta:{title:'商戶報(bào)表管理'}, component: () => import('../components/merchantManage/reportManage/index.vue') } ] },
隨后監(jiān)聽路由變化在watch中
watch:{ '$route'(val){ let flag = false this.openTab.forEach(tab => { if (val.path == tab.name) { this.$store.commit('set_active_index',val.path) flag = true return } }) if (!flag) { this.$store.commit('add_tabs', {name: val.path , title: val.meta.title}) this.$store.commit('set_active_index', val.path) } } },
上面的代碼大概意思就是,如果openTab中已經(jīng)存在這個(gè)路由,則直接設(shè)置高亮tab,如果不存在,則先添加路由信息到openTab中,然后再設(shè)置高亮
7. 當(dāng)前頁(yè)面刷新,需要保留一個(gè)tab也就是當(dāng)前頁(yè)的
mounted(){ this.$store.commit('add_tabs', {name: this.$route.path , title: this.$route.meta.title}) this.$store.commit('set_active_index', this.$route.path) }
設(shè)置tab的點(diǎn)擊事件
clickTab (tab) { this.$router.push({path: this.activeIndexTab}) }, removeTab (target) { //target當(dāng)前被點(diǎn)擊的name屬性 if (this.openTab.length == 1) { return } this.$store.commit('delete_tabs', target) if (this.activeIndexTab === target) { // 設(shè)置當(dāng)前激活的路由 if (this.openTab && this.openTab.length >= 1) { this.$store.commit('set_active_index', this.openTab[this.openTab.length - 1].name) this.$router.push({path: this.activeIndexTab}) } } },
最終完美實(shí)現(xiàn)想要的效果。
到此這篇關(guān)于vue實(shí)現(xiàn)el-menu和el-tab聯(lián)動(dòng)的示例代碼的文章就介紹到這了,更多相關(guān)el-menu和el-tab聯(lián)動(dòng)內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- vue實(shí)現(xiàn)el-menu與el-tabs聯(lián)動(dòng)的項(xiàng)目實(shí)踐
- vue之el-menu-item如何更改導(dǎo)航菜單欄選中的背景顏色
- Vue3 Element-plus el-menu無(wú)限級(jí)菜單組件封裝過(guò)程
- Vue中el-menu-item實(shí)現(xiàn)路由跳轉(zhuǎn)的完整步驟
- Vue?el-menu?左側(cè)菜單導(dǎo)航功能的實(shí)現(xiàn)
- vue+el-menu實(shí)現(xiàn)菜單欄無(wú)限多層級(jí)分類
- vue2+el-menu實(shí)現(xiàn)路由跳轉(zhuǎn)及當(dāng)前項(xiàng)的設(shè)置方法實(shí)例
- vue使用elementui的el-menu的折疊菜單collapse示例詳解
相關(guān)文章
如何修改element-ui中tree組件的icon圖標(biāo)(小白都會(huì)的前端技能)
這篇文章主要給大家介紹了關(guān)于如何修改element-ui中tree組件的icon圖標(biāo)的相關(guān)資料,本文介紹的是小白都會(huì)的前端技能,文中通過(guò)代碼以及圖文介紹的非常詳細(xì),需要的朋友可以參考下2024-01-01Vue實(shí)現(xiàn)下拉滾動(dòng)加載數(shù)據(jù)的示例
這篇文章主要介紹了Vue實(shí)現(xiàn)下拉滾動(dòng)加載數(shù)據(jù)的示例,幫助大家更好的理解和學(xué)習(xí)使用vue框架,感興趣的朋友可以了解下2021-04-04vue移動(dòng)端設(shè)置全屏背景的項(xiàng)目實(shí)踐
本vue移動(dòng)端項(xiàng)目設(shè)置全屏背景,關(guān)鍵是要找對(duì)文件,然后添加background屬性即可,文中通過(guò)示例代碼介紹的非常詳細(xì),需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2023-08-08如何通過(guò)URL來(lái)實(shí)現(xiàn)在Vue中存儲(chǔ)業(yè)務(wù)狀態(tài)實(shí)用技巧
這篇文章主要為大家介紹了如何通過(guò)URL來(lái)實(shí)現(xiàn)在Vue中存儲(chǔ)業(yè)務(wù)狀態(tài)實(shí)用技巧,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-10-10vue自定義權(quán)限標(biāo)簽詳細(xì)代碼示例
這篇文章主要給大家介紹了關(guān)于vue自定義權(quán)限標(biāo)簽的相關(guān)資料,在Vue中你可以通過(guò)創(chuàng)建自定義組件來(lái)實(shí)現(xiàn)自定義標(biāo)簽組件,文中給出了詳細(xì)的代碼示例,需要的朋友可以參考下2023-09-09vue預(yù)覽 pdf、word、xls、ppt、txt文件的實(shí)現(xiàn)方法
這篇文章主要介紹了vue預(yù)覽 pdf、word、xls、ppt、txt文件的實(shí)現(xiàn)方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-04-04