Vue + Elementui實(shí)現(xiàn)多標(biāo)簽頁(yè)共存的方法?相信很多沒(méi)有經(jīng)驗(yàn)的人對(duì)此束手無(wú)策,為此本文總結(jié)了問(wèn)題出現(xiàn)的原因和解決方法,通過(guò)這篇文章希望你能解決這個(gè)問(wèn)題。
創(chuàng)新互聯(lián)主營(yíng)南關(guān)網(wǎng)站建設(shè)的網(wǎng)絡(luò)公司,主營(yíng)網(wǎng)站建設(shè)方案,手機(jī)APP定制開(kāi)發(fā),南關(guān)h5小程序開(kāi)發(fā)搭建,南關(guān)網(wǎng)站營(yíng)銷推廣歡迎南關(guān)等地區(qū)企業(yè)咨詢關(guān)鍵邏輯
使用 keep-alive 來(lái)緩存各標(biāo)簽頁(yè)
通過(guò) vue-router 的 beforeEach 方法來(lái)更新標(biāo)簽信息
通過(guò) vuex 來(lái)保存標(biāo)簽信息
通過(guò) vuex 來(lái)使關(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)來(lái)不是主頁(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>
路由控制通過(guò)beforeEach來(lái)更新標(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
主容器通過(guò) closingPage 變量來(lái)及時(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>
看完上述內(nèi)容,你們掌握Vue + Elementui實(shí)現(xiàn)多標(biāo)簽頁(yè)共存的方法的方法了嗎?如果還想學(xué)到更多技能或想了解更多相關(guān)內(nèi)容,歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道,感謝各位的閱讀!
網(wǎng)站欄目:Vue+Elementui實(shí)現(xiàn)多標(biāo)簽頁(yè)共存的方法-創(chuàng)新互聯(lián)
網(wǎng)站地址:http://muchs.cn/article22/cdojcc.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供標(biāo)簽優(yōu)化、網(wǎng)站建設(shè)、品牌網(wǎng)站制作、定制開(kāi)發(fā)、靜態(tài)網(wǎng)站、定制網(wǎng)站
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)
猜你還喜歡下面的內(nèi)容