Vue + Elementui实现多标签页共存的方法?相信很多没有经验的人对此束手无策,为此本文总结了问题出现的原因和解决方法,通过这篇文章希望你能解决这个问题。
创新互联主营南关网站建设的网络公司,主营网站建设方案,手机APP定制开发,南关h5小程序开发搭建,南关网站营销推广欢迎南关等地区企业咨询关键逻辑
使用 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 { // 标签不存在,现在新建 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) // 关联路由与组件 const routes = [ { name: 'root', path: '/' }, { path: '/page', component: Page, children: [ { name: 'my', path: 'my', component: My, meta: { tabname: '个人主页' } } ] } ] // 创建路由器 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>
看完上述内容,你们掌握Vue + Elementui实现多标签页共存的方法的方法了吗?如果还想学到更多技能或想了解更多相关内容,欢迎关注创新互联行业资讯频道,感谢各位的阅读!
网站栏目:Vue+Elementui实现多标签页共存的方法-创新互联
网站地址:https://www.cdcxhl.com/article22/cdojcc.html
成都网站建设公司_创新互联,为您提供标签优化、网站建设、品牌网站制作、定制开发、静态网站、定制网站
声明:本网站发布的内容(图片、视频和文字)以用户投稿、用户转载内容为主,如果涉及侵权请尽快告知,我们将会在第一时间删除。文章观点不代表本网站立场,如需处理请联系客服。电话:028-86922220;邮箱:631063699@qq.com。内容未经允许不得转载,或转载时需注明来源: 创新互联