element?tab標(biāo)簽管理路由頁(yè)面的項(xiàng)目實(shí)踐
樣式

準(zhǔn)備
- 搭建好的vue腳手架(elementui,router,vuex)
- elementui(NavMenu 導(dǎo)航菜單,Tabs 標(biāo)簽頁(yè))
思路
- 將打開(kāi)的所有路由放到一個(gè)棧里(openTab:[]),tabs顯示遍歷openTab
- 初始狀態(tài),將首頁(yè)推入棧,并設(shè)置激活狀態(tài)
- 當(dāng)切換路由時(shí)(監(jiān)聽(tīng)路由變化),判斷棧里是否存在這個(gè)路由,若存在,只改變激活狀態(tài);若不存在,則推入棧,并改變激活狀態(tài)。
- tabs 切換,調(diào)用@tab-click='tabClick’方法,跳轉(zhuǎn)路由,(路由變化,走上一步中“若存在,只改變激活狀態(tài)”)
- tabs 移除,調(diào)用@tab-remove=‘tabRemove’ 方法,移除棧(openTab)中對(duì)應(yīng)的路由,若移除的路由是激活狀態(tài),那么跳轉(zhuǎn)路由到棧中最后一個(gè)(路由變化);若移除的路由非激活狀態(tài),不做修改
涉及到的內(nèi)容
vuex state:路由棧、激活狀態(tài) mutations: 添加、移除、修改激活狀態(tài)
watch
mounted
tab 切換、移除兩個(gè)方法
搭建
搭建頁(yè)面框架
slider組件
<template>
<el-menu
:default-active="$route.path"
class="slider"
background-color="#545c64"
text-color="#fff"
active-text-color="#ffd04b"
router
>
<el-menu-item index="/main" >
<i class="el-icon-menu"></i>
<span slot="title">首頁(yè)</span>
</el-menu-item>
<el-submenu v-for="(item,index) in menuList" :key="index" :index='item.id'>
<template slot="title">
<i :class="item.icon"></i>
<span>{{item.title}}</span>
</template>
<el-menu-item-group >
<el-menu-item v-for="child in item.children" :key="child.id" :index='child.index'>{{child.childtitle}}</el-menu-item>
</el-menu-item-group>
</el-submenu>
</el-menu>
</template>
<script>
export default {
name: 'Slider',
data(){
return {
menuList:[
{
id:'1',
title: '導(dǎo)航1',
icon:'el-icon-location',
children:[
{
index:'/page1',
childtitle:'導(dǎo)航1page1'
},
{
index:'/page2',
childtitle:'導(dǎo)航1page2'
},
]
},
{
id:'2',
title: '導(dǎo)航2',
icon:'el-icon-location',
children:[
{
index:'/page3',
childtitle:'導(dǎo)航2page3'
},
{
index:'/page4',
childtitle:'導(dǎo)航2page4'
},
]
}
]
}
},
}
</script>
<style scoped>
.slider{
height: 100vh;
}
</style>
home頁(yè)
<template>
<div class="home">
<el-row>
<el-col :span="4">
<!-- 左側(cè)導(dǎo)航欄 -->
<slider></slider>
</el-col>
<el-col :span='20'>
<!-- header -->
<nav-top></nav-top>
<!-- 內(nèi)容區(qū) -->
<div class="app-wrap">
<!-- 此處放置el-tabs代碼 -->
<div >
<el-tabs
v-model="activeIndex"
type="border-card"
closable
v-if="openTab.length"
@tab-click='tabClick'
@tab-remove='tabRemove'
>
<el-tab-pane
:key="item.name"
v-for="(item, index) in openTab"
:label="item.name"
:name="item.route"
>
</el-tab-pane>
</el-tabs>
</div>
<div class="content-wrap">
<router-view/>
</div>
</div>
</el-col>
</el-row>
</div>
</template>
通過(guò)路由配置,使頁(yè)面可以正常的跳轉(zhuǎn)
準(zhǔn)備狀態(tài)管理
state: {
openTab:[],//所有打開(kāi)的路由
activeIndex:'/main' //激活狀態(tài)
},
mutations: {
// 添加tabs
add_tabs (state, data) {
this.state.openTab.push(data);
},
// 刪除tabs
delete_tabs (state, route) {
let index = 0;
for (let option of state.openTab) {
if (option.route === route) {
break;
}
index++;
}
this.state.openTab.splice(index, 1);
},
// 設(shè)置當(dāng)前激活的tab
set_active_index (state, index) {
this.state.activeIndex = index;
},
},
在home頁(yè) ,或者silder頁(yè) , 初始的路由狀態(tài)
mounted () {
// 刷新時(shí)以當(dāng)前路由做為tab加入tabs
// 當(dāng)前路由不是首頁(yè)時(shí),添加首頁(yè)以及另一頁(yè)到store里,并設(shè)置激活狀態(tài)
// 當(dāng)當(dāng)前路由是首頁(yè)時(shí),添加首頁(yè)到store,并設(shè)置激活狀態(tài)
if (this.$route.path !== '/' && this.$route.path !== '/main') {
console.log('1');
this.$store.commit('add_tabs', {route: '/main' , name: 'main'});
this.$store.commit('add_tabs', {route: this.$route.path , name: this.$route.name });
this.$store.commit('set_active_index', this.$route.path);
} else {
console.log('2');
this.$store.commit('add_tabs', {route: '/main', name: 'main'});
this.$store.commit('set_active_index', '/main');
this.$router.push('/');
}
},
注意這里 如果你刷新 只想保留首頁(yè),那么 mounted 中 ,你只需寫else中的代碼。
如果刷新想,保留首頁(yè)和當(dāng)前路由頁(yè),if else都要寫()
監(jiān)聽(tīng)路由變化
watch:{
'$route'(to,from){
//判斷路由是否已經(jīng)打開(kāi)
//已經(jīng)打開(kāi)的 ,將其置為active
//未打開(kāi)的,將其放入隊(duì)列里
let flag = false;
for(let item of this.openTab){
console.log("item.name",item.name)
console.log("t0.name",to.name)
if(item.name === to.name){
console.log('to.path',to.path);
this.$store.commit('set_active_index',to.path)
flag = true;
break;
}
}
if(!flag){
console.log('to.path',to.path);
this.$store.commit('add_tabs', {route: to.path, name: to.name});
this.$store.commit('set_active_index', to.path);
}
}
}
tab方法
//tab標(biāo)簽點(diǎn)擊時(shí),切換相應(yīng)的路由
tabClick(tab){
console.log("tab",tab);
console.log('active',this.activeIndex);
this.$router.push({path: this.activeIndex});
},
//移除tab標(biāo)簽
tabRemove(targetName){
console.log("tabRemove",targetName);
//首頁(yè)不刪
if(targetName == '/'){
return
}
this.$store.commit('delete_tabs', targetName);
if (this.activeIndex === targetName) {
// 設(shè)置當(dāng)前激活的路由
if (this.openTab && this.openTab.length >= 1) {
console.log('=============',this.openTab[this.openTab.length-1].route)
this.$store.commit('set_active_index', this.openTab[this.openTab.length-1].route);
this.$router.push({path: this.activeIndex});
} else {
this.$router.push({path: '/'});
}
}
}
登錄 與 退出登錄

做登錄與退出時(shí) ,需要清空路由
退出登錄方法 或者 登錄成功方法 調(diào)用
this.$store.state.openTab = []; this.$store.state.activeIndex = '/main';
到此這篇關(guān)于element tab標(biāo)簽管理路由頁(yè)面的項(xiàng)目實(shí)踐的文章就介紹到這了,更多相關(guān)element tab標(biāo)簽管理路由頁(yè)面內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
淺析vue中常見(jiàn)循環(huán)遍歷指令的使用 v-for
這篇文章主要介紹了vue中常見(jiàn)循環(huán)遍歷指令的使用 v-for,包括v-for遍歷數(shù)組,v-for遍歷json對(duì)象,本文給大家介紹的非常詳細(xì),需要的朋友可以參考下2018-04-04
Vue關(guān)鍵字搜索功能實(shí)戰(zhàn)小案例
在vue項(xiàng)目中,搜索功能是我們經(jīng)常需要使用的一個(gè)場(chǎng)景,下面這篇文章主要給大家介紹了關(guān)于Vue關(guān)鍵字搜索功能的相關(guān)資料,文中通過(guò)實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下2023-06-06
Vue?和?Django?實(shí)現(xiàn)?Token?身份驗(yàn)證的流程
這篇文章主要介紹了Vue?和?Django?實(shí)現(xiàn)?Token?身份驗(yàn)證?,Vue.js?和?Django?編寫的前后端項(xiàng)目中,實(shí)現(xiàn)了基于?Token?的身份驗(yàn)證機(jī)制,其他前后端框架的?Token?實(shí)現(xiàn)原理與本文一致,需要的朋友可以參考下2022-08-08
vue轉(zhuǎn)react useEffect的全過(guò)程
這篇文章主要介紹了vue轉(zhuǎn)react useEffect的全過(guò)程,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-09-09
解決Vue2?axios發(fā)請(qǐng)求報(bào)400錯(cuò)誤"Error:?Request?failed?with?s
這篇文章主要給大家介紹了關(guān)于如何解決Vue2?axios發(fā)請(qǐng)求報(bào)400錯(cuò)誤"Error:?Request?failed?with?status?code?400"的相關(guān)資料,在Vue應(yīng)用程序中我們通常會(huì)使用axios作為網(wǎng)絡(luò)請(qǐng)求庫(kù),需要的朋友可以參考下2023-07-07
vue2.0結(jié)合Element實(shí)現(xiàn)select動(dòng)態(tài)控制input禁用實(shí)例
本篇文章主要介紹了vue2.0結(jié)合Element實(shí)現(xiàn)select動(dòng)態(tài)控制input禁用實(shí)例,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-05-05
el-tree的實(shí)現(xiàn)葉子節(jié)點(diǎn)單選的示例代碼
本文主要介紹了el-tree的實(shí)現(xiàn)葉子節(jié)點(diǎn)單選的示例代碼,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2022-08-08
vue中vxe-table虛擬滾動(dòng)列表的使用詳解
vxe-table 是一個(gè)功能強(qiáng)大的 Vue 表格組件,它支持虛擬滾動(dòng)列表作為其核心功能之一,本文主要介紹一下vxe-table的虛擬滾動(dòng)列表功能的使用場(chǎng)景和優(yōu)勢(shì),感興趣的可以了解下2023-12-12

