Vue + Elementui實現(xiàn)多標簽頁共存的方法
這個主題,早在一年前就已經(jīng)創(chuàng)建,也寫了一些內(nèi)容,礙于在應用上體驗始終不夠完美,一直只存著草稿。
經(jīng)過多個平臺實踐,多次迭代,一些功能加了又減了,最后還是回歸了最精簡的版本,已適用于大部分的場景,若有需要,可自行擴展。
關鍵邏輯
- 使用 keep-alive 來緩存各標簽頁
- 通過 vue-router 的 beforeEach 方法來更新標簽信息
- 通過 vuex 來保存標簽信息
- 通過 vuex 來使關閉頁不被緩存
核心代碼
定義 vuex 的跨頁變量(store/index.js)
import Vuex from 'vuex'
Vue.use(Vuex)
export default new Vuex.Store({
state: {
worktab: {
list: [
{
name: 'my',
tabname: '主頁',
path: '/page/my'
}
],
current: {
name: 'my',
tabname: '主頁',
path: '/page/my'
}
},
closingPage: ''
},
mutations: {
worktabRemove (state, p) {
// 關閉標簽
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) {
// 是當前頁,返回上一頁
router.back()
}
},
worktabRoute (state, p) {
let ind = state.worktab.list.findIndex(s => s.name === p.to.name)
if (ind > -1) {
// 標簽已存在
state.worktab.current = state.worktab.list[ind]
} else {
// 標簽不存在,現(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 標簽欄組件,在主容器引用
<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 () {
// 進來不是主頁時等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來更新標簽信息
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)
// 關聯(lián)路由與組件
const routes = [
{
name: 'root',
path: '/'
},
{
path: '/page',
component: Page,
children: [
{
name: 'my',
path: 'my',
component: My,
meta: {
tabname: '個人主頁'
}
}
]
}
]
// 創(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 變量來及時清理關閉頁面的緩存
<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實現(xiàn)多標簽頁共存的方法,希望對大家有所幫助,如果大家有任何疑問歡迎給我留言,小編會及時回復大家的!
相關文章
Vue中ElementUI分頁組件Pagination的使用方法
這篇文章主要為大家詳細介紹了Vue中ElementUI分頁組件Pagination的使用,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下2021-05-05
vue-quill-editor+plupload富文本編輯器實例詳解
這篇文章主要介紹了vue-quill-editor+plupload富文本編輯器實例詳解,需要的朋友可以參考下2018-10-10
Vue中一個組件調(diào)用其他組件的方法詳解(非父子組件)
vue中最常見子父組件產(chǎn)值,大家一定都很熟悉,最近項目中碰到非父組件中調(diào)用子組件方法的問題,這篇文章主要給大家介紹了關于Vue中一個組件調(diào)用其他組件的方法(非父子組件),需要的朋友可以參考下2022-10-10

