位置: IT常识 - 正文

Vue实现TagView标签导航栏缓存,解决重新进去页面不刷新问题(vue slot标签)

编辑:rootadmin
Vue实现TagView标签导航栏缓存,解决重新进去页面不刷新问题 实现的效果图

推荐整理分享Vue实现TagView标签导航栏缓存,解决重新进去页面不刷新问题(vue slot标签),希望有所帮助,仅作参考,欢迎阅读内容。

文章相关热门搜索词:vue tabs标签页,vue中的标签,vue slot标签,vue的title,vue tab标签页,vue的title,vue标签中怎么写表达式,vue中的标签,内容如对您有帮助,希望把文章链接给更多的朋友!

Vue实现TagView标签导航栏缓存,解决重新进去页面不刷新问题(vue slot标签)

一、先实现导航栏的效果1、先在@/layout/components/Sidebar目录下新建一个组件ScrollPane.vue,把下面代码复制进去<template> <div ref="scrollContainer" class="scroll-container" @wheel.prevent="handleScroll"> <div ref="scrollWrapper" class="scroll-wrapper" :style="{left: left + 'px'}"> <slot /> </div> </div></template><script>const padding = 15 // tag's paddingexport default { name: 'ScrollPane', data() { return { left: 0 } }, methods: { handleScroll(e) { const eventDelta = e.wheelDelta || -e.deltaY * 3// wheelDelta:-120;deltaY:-120 const $container = this.$refs.scrollContainer// 外面的container const $containerWidth = $container.offsetWidth// 外面的container的宽度 const $wrapper = this.$refs.scrollWrapper// 里面 const $wrapperWidth = $wrapper.offsetWidth// 里面的宽度 if (eventDelta > 0) { this.left = Math.min(0, this.left + eventDelta)// min() 方法可返回指定的数字中带有最低值的数字。 } else { if ($containerWidth - padding < $wrapperWidth) { if (this.left < -($wrapperWidth - $containerWidth + padding)) { this.left = 1 } else { this.left = Math.max(this.left + eventDelta, $containerWidth - $wrapperWidth - padding) } } else { this.left = 0 } } }, moveToTarget($target) { const $container = this.$refs.scrollContainer const $containerWidth = $container.offsetWidth const $targetLeft = $target.offsetLeft const $targetWidth = $target.offsetWidth if ($targetLeft < -this.left) { // tag in the left this.left = -$targetLeft + padding } else if ($targetLeft + padding > -this.left && $targetLeft + $targetWidth < -this.left + $containerWidth - padding) { // tag in the current view // eslint-disable-line } else { // tag in the right this.left = -($targetLeft - ($containerWidth - $targetWidth) + padding) } } }}</script><style rel="stylesheet/scss" lang="scss" scoped> .scroll-container { white-space: nowrap; position: relative; overflow: hidden; width: 100%; .scroll-wrapper { position: absolute; } }</style>2、在相同目录下再新建一个TagsView.vue组件,把下面代码复制进去<template> <div class="tags-view-container"> <scroll-pane ref="scrollPane" class="tags-view-wrapper"> <router-link v-for="tag in Array.from(visitedViews)" ref="tag" :key="tag.path" :class="isActive(tag)?'active':''" :to="tag" class="tags-view-item" @contextmenu.prevent.native="openMenu(tag,$event)" > {{ tag.title }} <span class="el-icon-close" @click.prevent.stop="closeSelectedTag(tag)" /> </router-link> </scroll-pane> <ul v-show="visible" class="contextmenu" :style="{left:left+'px',top:top+'px'}"> <li @click="closeSelectedTag(selectedTag)">关闭</li> <li @click="closeOthersTags">关闭其他</li> <li @click="closeAllTags">关闭所有</li> </ul> </div></template><script>import ScrollPane from './ScrollPane.vue'export default { name: 'TagsView', components: { ScrollPane }, data() { return { visible: false, top: 0, left: 0, selectedTag: {} } }, computed: { visitedViews() { return this.$store.state.tagsView.visitedViews } }, watch: { $route() { this.addViewTags() this.moveToCurrentTag() }, visible(value) { if (value) { document.body.addEventListener('click', this.closeMenu) } else { document.body.removeEventListener('click', this.closeMenu) } } }, mounted() { this.addViewTags() }, methods: { generateRoute() { if (this.$route.name) { return this.$route } return false }, isActive(route) { return route.path === this.$route.path }, addViewTags() { const route = this.generateRoute() if (!route) { return false } this.$store.dispatch('addVisitedViews', route) }, moveToCurrentTag() { const tags = this.$refs.tag this.$nextTick(() => { for (const tag of tags) { if (tag.to.path === this.$route.path) { this.$refs.scrollPane.moveToTarget(tag.$el) break } } }) }, closeSelectedTag(view) { this.$store.dispatch('delVisitedViews', view).then((views) => { if (this.isActive(view)) { const latestView = views.slice(-1)[0] if (latestView) { this.$router.push(latestView) } else { this.$router.push('/') } } }) }, closeOthersTags() { this.$router.push(this.selectedTag) this.$store.dispatch('delOthersViews', this.selectedTag).then(() => { this.moveToCurrentTag() }) }, closeAllTags() { this.$store.dispatch('delAllViews') this.$router.push('/') }, openMenu(tag, e) { this.visible = true this.selectedTag = tag const offsetLeft = this.$el.getBoundingClientRect().left // container margin left this.left = e.clientX - offsetLeft + 15 // 15: margin right this.top = e.clientY }, closeMenu() { this.visible = false } }}</script><style rel="stylesheet/scss" lang="scss" scoped> .tags-view-container { .tags-view-wrapper { background: #fff; height: 34px; border-bottom: 1px solid #d8dce5; box-shadow: 0 1px 3px 0 rgba(0, 0, 0, .12), 0 0 3px 0 rgba(0, 0, 0, .04); .tags-view-item { display: inline-block; position: relative; height: 26px; line-height: 26px; border: 1px solid #d8dce5; color: #495060; background: #fff; padding: 0 8px; font-size: 12px; margin-left: 5px; margin-top: 4px; &:first-of-type { margin-left: 15px; } &.active { background-color: #42b983; color: #fff; border-color: #42b983; &::before { content: ''; background: #fff; display: inline-block; width: 8px; height: 8px; border-radius: 50%; position: relative; margin-right: 2px; } } } } .contextmenu { margin: 0; background: #fff; z-index: 100; position: absolute; list-style-type: none; padding: 5px 0; border-radius: 4px; font-size: 12px; font-weight: 400; color: #333; box-shadow: 2px 2px 3px 0 rgba(0, 0, 0, .3); li { margin: 0; padding: 7px 16px; cursor: pointer; &:hover { background: #eee; } } } }</style><style rel="stylesheet/scss" lang="scss"> //reset element css of el-icon-close .tags-view-wrapper { .tags-view-item { .el-icon-close { width: 16px; height: 16px; vertical-align: 2px; border-radius: 50%; text-align: center; transition: all .3s cubic-bezier(.645, .045, .355, 1); transform-origin: 100% 50%; &:before { transform: scale(.6); display: inline-block; vertical-align: -3px; } &:hover { background-color: #b4bccc; color: #fff; } } } }</style>3、在store文件夹下新建一个tagsView.js,同样把复制代码进去const tagsView = { state: { visitedViews: [], cachedViews: [] }, mutations: { ADD_VISITED_VIEWS: (state, view) => { if (state.visitedViews.some(v => v.path === view.path)) return state.visitedViews.push(Object.assign({}, view, { title: view.meta.title || 'no-name' })) if (!view.meta.noCache) { state.cachedViews.push(view.name) } }, DEL_VISITED_VIEWS: (state, view) => { for (const [i, v] of state.visitedViews.entries()) { if (v.path === view.path) { state.visitedViews.splice(i, 1) break } } for (const i of state.cachedViews) { if (i === view.name) { const index = state.cachedViews.indexOf(i) state.cachedViews.splice(index, 1) break } } }, DEL_OTHERS_VIEWS: (state, view) => { for (const [i, v] of state.visitedViews.entries()) { if (v.path === view.path) { state.visitedViews = state.visitedViews.slice(i, i + 1) break } } for (const i of state.cachedViews) { if (i === view.name) { const index = state.cachedViews.indexOf(i) state.cachedViews = state.cachedViews.slice(index, i + 1) break } } }, DEL_ALL_VIEWS: (state) => { state.visitedViews = [] state.cachedViews = [] } }, actions: { addVisitedViews({ commit }, view) { commit('ADD_VISITED_VIEWS', view) }, delVisitedViews({ commit, state }, view) { return new Promise((resolve) => { commit('DEL_VISITED_VIEWS', view) resolve([...state.visitedViews]) }) }, delOthersViews({ commit, state }, view) { return new Promise((resolve) => { commit('DEL_OTHERS_VIEWS', view) resolve([...state.visitedViews]) }) }, delAllViews({ commit, state }) { return new Promise((resolve) => { commit('DEL_ALL_VIEWS') resolve([...state.visitedViews]) }) } }}export default tagsView4、在store/index.js中引入tagsView.jsimport Vue from 'vue'import Vuex from 'vuex'import getters from './getters'// 自定义模块import app from './modules/app'import settings from './modules/settings'import user from './modules/user'import permission from './modules/permission'import tagsView from './modules/tagsView'Vue.use(Vuex)const store = new Vuex.Store({ modules: { app, settings, user, permission, tagsView }, getters})export default store5、在@/layout/的主组件中引入TagsView组件

二、实现重新返回页面时不刷新,保留原本页面内容1、在@/layout/components/Sidebar下的AppMain.vue中添加如下代码

主要是通过keep-alive 和 router-view结合来实现

<template> <section class="app-main"> <transition name="fade-transform" mode="out-in"> <keep-alive :include="cachedViews"> //这是新增加的 <router-view :key="key" /> </keep-alive> // </transition> </section></template><script>export default { name: 'AppMain', computed: { key() { return this.$route.path }, cachedViews() {//这是新增加的 return this.$store.state.tagsView.cachedViews }// }}</script>2、在route/index.js中为页面路由添加keepAlive为true,例如在首页 { path: '/', component: Layout, redirect: '/dashboard', children: [{ path: 'dashboard', name: 'Dashboard', component: () => import('@/views/dashboard/index'), //在meta对象中添加key值keepAlive为true meta: { title: '首页', icon: 'dashboard' ,keepAlive:true} }] }3、还有一点很重要!!要确保组件的命名和路由的命名是一样的,不然页面不会保留原本内容,会进行自动刷新

4、退出登录时,要清除缓存可以在store/modules/user.js中添加如下代码import store from '..'// user logout logout({ commit, state }) { return new Promise((resolve, reject) => { logout().then(() => { removeToken() resetRouter() store.dispatch('delAllViews') //增加多这行 commit('RESET_STATE') resolve() }).catch(error => { reject(error) }) }) },最后,这是项目的目录结构,红色框是新建的文件,蓝色是修改增加的文件

 

本文链接地址:https://www.jiuchutong.com/zhishi/277333.html 转载请保留说明!

上一篇:本地连接没有了怎么办(本地连接没有了怎么恢复)

下一篇:“讲述人”功能(讲述人功能怎么关闭)

  • iqoo8怎么分屏(iqoo,怎么分屏)

    iqoo8怎么分屏(iqoo,怎么分屏)

  • 网易云唱片收藏架怎么编辑(网易云唱片收藏架怎么换)

    网易云唱片收藏架怎么编辑(网易云唱片收藏架怎么换)

  • 拼多多口令复制后怎么助力(拼多多口令复制后打不开)

    拼多多口令复制后怎么助力(拼多多口令复制后打不开)

  • 苹果11怎么更新系统(苹果11怎么更新软件)

    苹果11怎么更新系统(苹果11怎么更新软件)

  • excel文档怎么筛选出想要的(excel文档怎么筛选出重复的电话号)

    excel文档怎么筛选出想要的(excel文档怎么筛选出重复的电话号)

  • 优酷可以同时登录几个设备

    优酷可以同时登录几个设备

  • 手机不小心入水怎么办(手机不小心进了点水)

    手机不小心入水怎么办(手机不小心进了点水)

  • 磁盘占用100%有什么影响(磁盘占用100%是中毒了吗)

    磁盘占用100%有什么影响(磁盘占用100%是中毒了吗)

  • 你的硬件设置已更改,请重启计算机(你的硬件设置已更改,请重启计算机 卡住)

    你的硬件设置已更改,请重启计算机(你的硬件设置已更改,请重启计算机 卡住)

  • 苹果手机微信锁屏后没有声音提示(苹果手机微信锁屏没有提示音怎么回事)

    苹果手机微信锁屏后没有声音提示(苹果手机微信锁屏没有提示音怎么回事)

  • h310主板能装win7系统吗(h310主板能装什么显卡)

    h310主板能装win7系统吗(h310主板能装什么显卡)

  • dtlfolder是什么文件夹(fileld是什么意思中文)

    dtlfolder是什么文件夹(fileld是什么意思中文)

  • 冷屏和暖屏怎么区分(冷屏和暖屏哪个清晰度好)

    冷屏和暖屏怎么区分(冷屏和暖屏哪个清晰度好)

  • internet传统的4种服务功能(internet传统的主要服务功能有什么)

    internet传统的4种服务功能(internet传统的主要服务功能有什么)

  • 固态硬盘1t是什么意思(固态硬盘1t和1tb的区别)

    固态硬盘1t是什么意思(固态硬盘1t和1tb的区别)

  • 小米10听筒听不到声音(小米听筒听不见免提能听见)

    小米10听筒听不到声音(小米听筒听不见免提能听见)

  • exynos 980处理器是啥水平(Exynos 980处理器相当于)

    exynos 980处理器是啥水平(Exynos 980处理器相当于)

  • 爱奇艺积分有什么用(爱奇艺积分有什么作用)

    爱奇艺积分有什么用(爱奇艺积分有什么作用)

  • 腾讯会议是视频吗(zoom视频会议)

    腾讯会议是视频吗(zoom视频会议)

  • 华为p30屏幕上有没有音量键(华为p30屏幕上有个圆圈怎么去掉)

    华为p30屏幕上有没有音量键(华为p30屏幕上有个圆圈怎么去掉)

  • iphone11圆点怎么弄出来(iphone11圆点怎么设置双击截图)

    iphone11圆点怎么弄出来(iphone11圆点怎么设置双击截图)

  • 抖音号没绑定怎么登录(抖音号没绑定怎么登录不了)

    抖音号没绑定怎么登录(抖音号没绑定怎么登录不了)

  • 抖音多少粉丝能直播(抖音多少粉丝能建粉丝群)

    抖音多少粉丝能直播(抖音多少粉丝能建粉丝群)

  • iphone8plus如何强制横屏(iphone8plus如何强制恢复)

    iphone8plus如何强制横屏(iphone8plus如何强制恢复)

  • 荣耀v20支持生活防水吗(荣耀v20yoyo)

    荣耀v20支持生活防水吗(荣耀v20yoyo)

  • word文档图标变为白色了(word文档图标变成空白)

    word文档图标变为白色了(word文档图标变成空白)

  • linux系统很卡的基本排查方法介绍(linux太卡顿)

    linux系统很卡的基本排查方法介绍(linux太卡顿)

  • 怎么查19年以前税收
  • 个人所得税的计提和缴纳分录怎么做
  • 维修费开发票选择什么税收分类编码
  • 小规模纳税人变成一般纳税人之前的发票变更
  • 部分红冲的发票,剩余未冲红部分可以再红冲么
  • 房地产股权收购案例
  • 应付账款少记违反什么认定
  • 税后利润再投资
  • 销售退回 账务处理
  • 在建工程转固后又发生成本
  • 房地产销售税额抵减
  • 国外公司进口能退税吗
  • 委托贷款利息收入现金流
  • 全额抵扣有哪些
  • 银行处置以物抵债违法吗
  • 归还银行贷款利息分录
  • 公户没有开票进账了可以吗
  • 总额法和净额法哪个合理
  • 企业所得税季度申报表A类
  • win7系统没有桌面图标怎么办
  • 电费预提费用
  • debian10.9安装教程
  • php pdo分页
  • ie浏览器进程
  • 自创商誉的确认
  • 应收票据包括哪些支票
  • 进出口公司出口退税额
  • 什么情况下计提信用减值损失
  • 建筑安装企业核定征收企业所得税
  • 单位缴费基数申报提交了能改吗
  • php循环字符串
  • php使用什么开发工具
  • 微软的人工智能ChatGPT
  • php网页聊天室
  • 可供出售金融资产的会计处理
  • python socket连接后发送数据被强行终止
  • 织梦如何使用
  • 企业的净资产包括固定资产吗
  • 乘客人身意外保险单可以报销吗
  • 企业转让无形资产的方式有
  • 资产负债表的固定资产怎么算出来的
  • 预收账款的核算处理
  • 已付款发票未到怎么做分录
  • 无形资产土地入账日期怎么确定
  • 工程物资原材料在资产负债表哪个科目体现
  • 软件续费应记什么科目
  • 公司向法人借款会计分录
  • 不动产进项税额转出
  • 电子发票一定要盖章吗
  • 建筑公司劳务费怎么做账
  • mysql数据库分区查询
  • java连接mysql数据库教程
  • vmware如何用
  • win7怎么隐藏我的电脑
  • linux的sh
  • OS X 10.12.6 beta 1如何更新 OS X 10.12.6 beta 1如何升级
  • gpt分区方法
  • 求windows7用户账户
  • 进程 com surrogate
  • win7开始菜单找不到启动项
  • win8.1无法进入系统
  • win10 20h2怎么装
  • 戴尔笔记本自带win7系统盘重装系统
  • bootstraptable教程
  • php autoload
  • node创建服务
  • bat脚本如何运行
  • python编程术语
  • python如何调用c程序
  • javascript核心技术
  • androiddialog圆角
  • js设置rem
  • jquery轮播图代码
  • jquery兼容ie
  • jquery选择器的分类有哪些
  • 国家税务总局是正部级还是副部级
  • 定额发票查询入口在哪里
  • 青岛市高新区是红岛吗
  • 个人土地建房
  • 增值税普通发票和电子普通发票的区别
  • 免责声明:网站部分图片文字素材来源于网络,如有侵权,请及时告知,我们会第一时间删除,谢谢! 邮箱:opceo@qq.com

    鄂ICP备2023003026号

    网站地图: 企业信息 工商信息 财税知识 网络常识 编程技术

    友情链接: 武汉网站建设