Vue + Elementui實(shí)現(xiàn)多標(biāo)簽頁(yè)共存的方法
這個(gè)主題,早在一年前就已經(jīng)創(chuàng)建,也寫了一些內(nèi)容,礙于在應(yīng)用上體驗(yàn)始終不夠完美,一直只存著草稿。
經(jīng)過多個(gè)平臺(tái)實(shí)踐,多次迭代,一些功能加了又減了,最后還是回歸了最精簡(jiǎn)的版本,已適用于大部分的場(chǎng)景,若有需要,可自行擴(kuò)展。
關(guān)鍵邏輯
- 使用 keep-alive 來緩存各標(biāo)簽頁(yè)
- 通過 vue-router 的 beforeEach 方法來更新標(biāo)簽信息
- 通過 vuex 來保存標(biāo)簽信息
- 通過 vuex 來使關(guān)閉頁(yè)不被緩存
核心代碼
定義 vuex 的跨頁(yè)變量(store/index.js)
import Vuex from 'vuex'
Vue.use(Vuex)
export default new Vuex.Store({
state: {
worktab: {
list: [
{
name: 'my',
tabname: '主頁(yè)',
path: '/page/my'
}
],
current: {
name: 'my',
tabname: '主頁(yè)',
path: '/page/my'
}
},
closingPage: ''
},
mutations: {
worktabRemove (state, p) {
// 關(guān)閉標(biāo)簽
let ind = state.worktab.list.findIndex(s => s.name === p)
if (ind > -1) {
// 清理 keep alive - start
state.closingPage = state.worktab.list[ind].name
// 清理 keep alive - end
state.worktab.list.splice(ind, 1)
}
if (p === state.worktab.current.name) {
// 是當(dāng)前頁(yè),返回上一頁(yè)
router.back()
}
},
worktabRoute (state, p) {
let ind = state.worktab.list.findIndex(s => s.name === p.to.name)
if (ind > -1) {
// 標(biāo)簽已存在
state.worktab.current = state.worktab.list[ind]
} else {
// 標(biāo)簽不存在,現(xiàn)在新建
state.worktab.list.push(p.to)
state.worktab.current = p.to
}
state.closingPage = ''
}
},
actions: {
worktabRemove ({commit}, p) {
commit('worktabRemove', p)
},
worktabRoute ({commit}, p) {
commit('worktabRoute', p)
}
},
strict: debug
})
定義 worktab 標(biāo)簽欄組件,在主容器引用
<template>
<div class="cp-worktab">
<el-tabs v-model="activeTab" type="card" @tab-remove="removeTab" @tab-click="clickTab">
<el-tab-pane
v-for="t in worktabs"
:key="t.name"
:label="t.tabname"
:name="t.name"
:closable="t.name !== 'my'"
>
</el-tab-pane>
</el-tabs>
</div>
</template>
<script>
export default {
created () {
// 進(jìn)來不是主頁(yè)時(shí)等list加載后再更新一次current
setTimeout(() => {
this.activeTab = this.$store.state.worktab.current.name
}, 500)
},
watch: {
'$store.state.worktab.current' (tab) {
this.activeTab = tab.name
}
},
computed: {
worktabs () {
return this.$store.state.worktab.list
}
},
data () {
return {
activeTab: 'name'
}
},
methods: {
clickTab (tab) {
this.$router.push(this.worktabs[1 * tab.index].path)
},
removeTab (name) {
this.$store.dispatch('worktabRemove', name)
}
}
}
</script>
路由控制通過beforeEach來更新標(biāo)簽信息
import Vue from 'vue'
import VueRouter from 'vue-router'
import store from '@/store'
import Page from '../components/console/Page.vue'
import My from '../components/console/My.vue'
Vue.use(VueRouter)
// 關(guān)聯(lián)路由與組件
const routes = [
{
name: 'root',
path: '/'
},
{
path: '/page',
component: Page,
children: [
{
name: 'my',
path: 'my',
component: My,
meta: {
tabname: '個(gè)人主頁(yè)'
}
}
]
}
]
// 創(chuàng)建路由器
const router = new VueRouter({
routes
})
router.beforeEach((to, from, next) => {
next()
store.dispatch('worktabRoute', {
to: {
name: to.name ? to.name : '',
tabname: (to.meta && to.meta.tabname) ? to.meta.tabname : '',
path: to.path
},
from: {
name: from.name ? from.name : '',
tabname: (from.meta && from.meta.tabname) ? from.meta.tabname : '',
path: from.path
}
})
return
})
export default router
主容器通過 closingPage 變量來及時(shí)清理關(guān)閉頁(yè)面的緩存
<template>
<div>
<cp-worktab></cp-worktab>
<div class="cp-content">
<keep-alive :exclude="closingPage">
<router-view></router-view>
</keep-alive>
</div>
</div>
</template>
<script>
import Worktab from '../module/Worktab'
export default {
components: {
cpWorktab: Worktab
},
data () {
return {}
},
computed: {
closingPage () {
return this.$store.state.closingPage
}
}
}
</script>
總結(jié)
以上所述是小編給大家介紹的Vue + Elementui實(shí)現(xiàn)多標(biāo)簽頁(yè)共存的方法,希望對(duì)大家有所幫助,如果大家有任何疑問歡迎給我留言,小編會(huì)及時(shí)回復(fù)大家的!
相關(guān)文章
前端Vue?select下拉框使用以及監(jiān)聽事件詳解
由于前端項(xiàng)目使用的是Vue.js和bootstrap整合開發(fā),中間用到了select下拉框,這篇文章主要給大家介紹了關(guān)于前端Vue?select下拉框使用以及監(jiān)聽事件的相關(guān)資料,需要的朋友可以參考下2024-03-03
Vue3.0版本強(qiáng)勢(shì)升級(jí)點(diǎn)特性詳解
這篇文章主要介紹了Vue3.0版本強(qiáng)勢(shì)升級(jí)點(diǎn)特性詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪<BR>2022-06-06
Vue手機(jī)號(hào)正則匹配姓名加密展示功能的實(shí)現(xiàn)
這篇文章主要介紹了Vue手機(jī)號(hào)正則匹配,姓名加密展示,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2022-08-08
vue循環(huán)el-button實(shí)現(xiàn)點(diǎn)擊哪個(gè)按鈕,那個(gè)按鈕就變色
這篇文章主要介紹了vue循環(huán)el-button實(shí)現(xiàn)點(diǎn)擊哪個(gè)按鈕,那個(gè)按鈕就變色問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-10-10
vue前端獲取不同客戶端mac地址最詳細(xì)步驟(避免踩坑)
在開發(fā)過程中,綁定賬號(hào)和電腦的功能可以通過獲取電腦的MAC地址實(shí)現(xiàn),下面這篇文章主要介紹了vue前端獲取不同客戶端mac地址的相關(guān)資料,文中通過代碼介紹的非常詳細(xì),需要的朋友可以參考下2024-10-10
vue如何動(dòng)態(tài)修改meta的title
這篇文章主要介紹了vue如何動(dòng)態(tài)修改meta的title,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-06-06

