清除keep-alive缓存,动态缓存
这个题目有点误导人,但是方向大致是这么个方向,其实不叫清除,而叫动态添加缓存。那么现在说下项目背景,用了vue2.0的后台系统,elementui再加之前没有删干净的乱七八糟的elment-admin的东西。需求嘛就是router外面套了keep-alive,就是从左边的侧边栏点击添加到窗口的很多tab页,切换时候要有缓存,关闭时候应该清掉缓存,现在就是关闭时候也有缓存导致再次打开时候上次的信息还在。看了网上很多强制清缓存的东西,大致意思就是从搞不清楚什么层级的对象里面找cache这个对象再清除掉,然后还有个什么key,我找了一天没找明白,我就换了个思路,特此记录下,给后面也有相同需求的人。
一,找到你写keep-alive的文件。改造成大致这样:
<template><section class="app-main"><transition name="fade-transform" mode="out-in" v-if="$route.meta.keepAlive"><keep-alive><router-view :key="key" /></keep-alive></transition><transition name="fade-transform" mode="out-in" v-else><router-view :key="key" /></transition></section>
</template>
意思嘛就是说你的路由中的meta的keepAlive是true的用缓存,是false的不用。
二,然后你就需要到你的router的文件里面去吧这个叫keepaAlive的全部false。我们是动态添加嘛,这里相当于一个init
就像这样:
{path:'/franchisee',component: Layout,redirect: 'noredirect',children:[{path: 'franchisee-view',name: 'franchisee-view',component: () =>import('@/views/franchisee/franchisee-view'),meta: {title: '加盟商管理',icon: 'dashboard',keepAlive: false},},{path: 'franchisee-detail',name: 'franchisee-detail',component: () =>import('@/views/franchisee/franchisee-detail'),meta: {title: '加盟商详情',icon: 'dashboard',keepAlive: false},},{path: 'franchisee-add',name: 'franchisee-add',component: () =>import('@/views/franchisee/franchisee-add'),meta: {title: '新增加盟商',icon: 'dashboard',keepAlive: false},},{
全部写false
三,然后去你TagView.vue文件(没有的话意思就是说你点击你页面的那个tag标签的那个页面上的方法的文件)找到以下两个方法,一个是添加,一个是关闭,没有的话自己写一个。
addViewTags() {const {name} = this.$route;if (name) {this.$store.dispatch("addView", this.$route);this.$store.dispatch("addCachedView", this.$route);}this.$route.meta.keepAlive = truereturn false;},
起作用的主要就是这个this.$route.meta.keepAlive=true这个方法,意思就是你点开一个页面tag就动态置为true,只要你不关闭,切换时候它还是true。这样缓存就实现啦。
四,关的时候清除
还是在刚刚的文件上,关闭的时候把刚刚那个属性设置为false就行啦
closeSelectedTag(view) {if (this.isActive(view)) { view.meta.keepAlive = false }this.$store.dispatch("delCachedViewThis", this)console.log(this)this.$store.dispatch("delView", view).then(({visitedViews}) => {if (this.isActive(view)) {const latestView = visitedViews.slice(-1)[0];if (latestView) {this.$router.push(latestView);} else {this.$router.push("/");}}});},
下面的那些你们都可以不用看 主要是if (this.isActive(view)) { view.meta.keepAlive = false }这句话。哦。调用了方法是吧?我一起贴上来。
isActive(route) {return route.path === this.$route.path;},
再附上结构的代码
<div class="tags-view-container"><scroll-pane ref="scrollPane" class="tags-view-wrapper"><div class="a-flex-rfsfe"><router-link v-for="tag in visitedViews" ref="tag" :class="isActive(tag) ? 'active' : ''":to="{ path: tag.path, query: tag.query, fullPath: tag.fullPath }" :key="tag.path"class="tags-view-item " style="margin-left: 4px;" @click.middle.native="closeSelectedTag(tag)"@contextmenu.prevent.native="openMenu(tag, $event)"><span>{{ tag.title }}</span><!-- <span v-show="visitedViews.length > 1" --><span class="el-icon-close a-ml-08" style="width: 9px; height: 9px; padding-bottom: 3px"@click.prevent.stop="closeSelectedTag(tag)" /></router-link></div></scroll-pane>
其实这里看具体的代码没有意义。毕竟大家的项目都不同,不可能照抄。掌握到思想的精髓就行了。意思就是初始时候全部置为false没有缓存,然后添加tag的时候动态改为true,关闭时候再改为false。这样就比你强制去清除那个什么cache简单多啦。
就这,希望能帮到大家。
后续。后来,发现这个方法也是不可行的,这样的话,需要缓存和不需要缓存的就无法区分开来了。记录下此问题的解决办法。就是搞个白名单,需要缓存的放入到白名单里面,然后在需要缓存的路由的meta里添加一个新属性,如果是关闭的话就把这个属性添加上,就不需要缓存了,如果没有这个属性又在白名单中的话就需要缓存。新开一个窗口先判断有没有,有并在白名单中,就添加上缓存属性,切换的之前删掉,切换的时候就是又没有了。白名单加动态删除添加属性来双重控制。上代码。
layout/components/TagsView.vue
export default {components: {ScrollPane,},data() {return {visible: false,top: 0,left: 0,selectedTag: {},whiteList: ['project-add', 'station-add', 'customer-add', 'franchisee-add', 'station-apply', 'station-construction', 'price-edit', 'project-confirmed', 'device-add', 'device-edit', 'device-type-edit', 'bank-card-add', 'staff-add', 'staff-exit', 'franchisee-change']};},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: {// generateTitle, // generateTitle by vue-i18nisActive(route) {return route.path === this.$route.path;},addViewTags() {const {name} = this.$route;this.$route.meta.keepAlive = false;let isKeep = this.whiteList.filter((item, index) => { return item == name });if (isKeep.length == 1 && !this.$route.meta.hasOwnProperty('hash')) {this.$route.meta.keepAlive = true;}if (name) {this.$store.dispatch("addView", this.$route);this.$store.dispatch("addCachedView", this.$route);}if (this.$route.meta.hasOwnProperty('hash')) {delete this.$route.meta.hash;}return false;},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);// this.$route.meta.keepAlive = true// when query is different then updateif (tag.to.fullPath !== this.$route.fullPath) {this.$store.dispatch("updateVisitedView", this.$route);}break;}}});},refreshSelectedTag(view) {console.log("view123", view);this.$store.dispatch("delCachedViewThis", this).then(() => {const {fullPath} = view;console.log("fullPath", fullPath);this.$nextTick(() => {this.$router.replace({path: fullPath,});});});},closeSelectedTag(view) {if (this.isActive(view)) {view.meta.keepAlive = false;view.meta.hash = "del";}this.$store.dispatch("delCachedViewThis", this)console.log(this)this.$store.dispatch("delView", view).then(({visitedViews}) => {if (this.isActive(view)) {const latestView = visitedViews.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) {const menuMinWidth = 105;const offsetLeft = this.$el.getBoundingClientRect().left; // container margin leftconst offsetWidth = this.$el.offsetWidth; // container widthconst maxLeft = offsetWidth - menuMinWidth; // left boundaryconst left = e.clientX - offsetLeft + 15; // 15: margin rightif (left > maxLeft) {this.left = maxLeft;} else {this.left = left;}this.top = e.clientY;this.visible = true;this.selectedTag = tag;},closeMenu() {this.visible = false;},},
};
清除keep-alive缓存,动态缓存相关推荐
- nginx 缓存动态内容 和使用自定义错误503
2019独角兽企业重金招聘Python工程师标准>>> 安装时添加 ngx_cache_purge 模块 ./configure --user=www --group=www --p ...
- ThinkPHP实现静态缓存和动态缓存
2019独角兽企业重金招聘Python工程师标准>>> 弄清静态缓存和动态缓存的用途: 静态缓存:生成静态页面--缓存的是整个页面; 动态缓存:仅对数据库中的数据进行了缓存,即&qu ...
- spring_在Spring中使用多个动态缓存
spring 在第三篇有关Spring(很长一段时间)中缓存管理器的文章中,我想通过展示如何配置多个动态创建缓存的缓存管理器来扩展前 两个. Spring具有CompositeCacheManager ...
- 微信小程序开发工具 清除授权缓存/文件缓存/登录缓存等等
今天2.19.3.25 在开发微信小程序时,作为测试号想清除授权缓存,一直没有找到方法, 最后无意中看到了解决方法 微信小程序开发工具 清除授权缓存/文件缓存/登录缓存等等.完美解决
- 浅析ThinkPHP缓存之快速缓存(F方法)和动态缓存(S方法)
系统默认的缓存方式是采用File方式缓存,我们可以在项目配置文件里面定义其他的缓存方式,例如,修改默认的缓存方式为Xcache(当然,你的环境需要支持Xcache) 对于File方式缓存下的缓存目录下 ...
- 在Spring中使用多个动态缓存
在第三篇有关Spring(长时间)的缓存管理器的文章中,我想通过展示如何配置多个动态创建缓存的缓存管理器来扩展前 两个 . Spring具有CompositeCacheManager ,从理论上讲,它 ...
- ecshop清除mysql缓存_ECSHOP 缓存问题的解决方法
ECSHOP的缓存存放在templates/caches/文章夹下,时间长了这个文件夹就会非常庞大,拖慢网站速度.还有很多情况我们不需要他的缓存.本文介绍禁用ECSHOP缓存的方法. ECSHOP的缓 ...
- 谷粒商城 Day09 首页分类与SpEL动态缓存切面
Day09 首页分类与SpEL动态缓存切面 一.优化缓存逻辑 百万并发进来,判断 bloomFilter 和缓存中拿,先执行哪个最好?1. 先布隆 ,再缓存 面对攻击 1 好 2. 先缓存 ,再布隆 ...
- 清除 Google Chrome 的 DNS 缓存
要清除 Google Chrome 的 DNS 缓存,尝试下列(1)或(2):. 清除缓存:chrome地址栏输入 chrome://net-internals/#dns ,点击 "清除主机 ...
- 静态缓存和动态缓存的比较
静态页面的缓存可能有2种形式:其实主要区别就是CMS是否自己负责关联内容的缓存更新管理. 静态缓存:是在新内容发布的同时就立刻生成相应内容的静态页面,比如:2003年3月22日,管理员通过后台内容管理 ...
最新文章
- ffmpeg4编解码例子
- 头插法和尾插法分别建立链表(复制即可应用)
- struts2教程--实现文件上传下载
- 在WPF程序中使用多线程技术
- Codeup-问题 A: 【字符串】最长回文子串
- python算24点穷举法_关于24点去重的算法?
- 深度学习中的正则化技术详解
- jfinal分页时使用like
- c# rar解压大小_C#中使用WinRAR实现加密压缩及解压缩文件
- Java类图(记录/转)
- 什么样的公司程序员待遇好
- 电脑设置U盘启动快捷键
- 【面试】AI常见的面试问题
- 三、Oracle/支付宝/旺旺
- OOA/OOD/OOP(了解)
- 城市槽音乐在津巴布韦的美国音乐如何影响其他文化和身份的个案研究
- 计算机科学的发展及其介绍,计算机科学与技术专业发展历史介绍
- AAAI 2022主题论文推荐——Semantic Segmentation
- javascript 实现购物车页面
- FileZilla使用手册(MAC版)