bus使用清除keepalive缓存

javascript 复制代码
<template>
  <div id="app">
    <!-- <keep-alive>
      <router-view v-if="$route.meta.noCache"></router-view>
    </keep-alive>
    <router-view v-if="!$route.meta.noCache"></router-view> -->

    <keep-alive>
      <router-view v-if="$route.meta.keepAlive" :key="fullPath"></router-view>
    </keep-alive>
    <router-view v-if="!$route.meta.keepAlive"></router-view>
  </div>
</template>

<script>
export default {
  name: 'App',
  provide() {
    return {
      reload: this.reload
    }
  },
  computed: {
    fullPath() {
      // console.log(this.$route.fullPath);
      return this.$route.fullPath
    }
  },
  mounted() {
    // 注册监听全局的clearKeepAlive方法,可在其他组件中触发此方法
    this.$bus.$on('clearKeepAlive', this.clearKeepAlive)

    // 使用
    // this.$bus.emit("clearKeepAlive", path); // 清除缓存
  },
  methods: {
    // 根据fullUrl清除keepAlive
    clearKeepAlive(fullUrl) {
      // console.log('bus触发要清除的keepAlive', fullUrl);
      this.$children.forEach((item) => {
        if (item.$vnode.data.key == fullUrl) {
          console.log(item.$vnode.data.key)
          this._myDestory(item)
        }
      })
    },
    // 封装清除某个组件的keepAlive状态,并销毁
    _myDestory(keepAliveComponent) {
      // 你可以根据自己的业务更改此处的判断逻辑,酌情决定是否摧毁本层缓存。
      if (keepAliveComponent.$vnode && keepAliveComponent.$vnode.data.keepAlive) {
        if (
          keepAliveComponent.$vnode.parent &&
          keepAliveComponent.$vnode.parent.componentInstance &&
          keepAliveComponent.$vnode.parent.componentInstance.cache
        ) {
          if (keepAliveComponent.$vnode.componentOptions) {
            var key =
              keepAliveComponent.$vnode.key == null
                ? keepAliveComponent.$vnode.componentOptions.Ctor.cid +
                  (keepAliveComponent.$vnode.componentOptions.tag
                    ? `::${keepAliveComponent.$vnode.componentOptions.tag}`
                    : '')
                : keepAliveComponent.$vnode.key
            var cache = keepAliveComponent.$vnode.parent.componentInstance.cache
            var keys = keepAliveComponent.$vnode.parent.componentInstance.keys
            if (cache[key]) {
              if (keys.length) {
                var index = keys.indexOf(key)
                if (index > -1) {
                  keys.splice(index, 1)
                }
              }
              delete cache[key]
            }
          }
        }
      }
      keepAliveComponent.$destroy()
    },
    reload() {
      this.ifRouterAlive = false
      this.$nextTick(() => {
        this.ifRouterAlive = true
      })
    }
  }
}
</script>

<style lang="scss" scoped></style>
相关推荐
一只小风华~1 分钟前
HTML前端开发:JavaScript 常用事件详解
前端·javascript·html
Revol_C4 分钟前
【调试日志】我只是用wangeditor上传图片而已,页面咋就崩溃了呢~
前端·vue.js·程序员
天天码行空7 分钟前
GruntJS-前端自动化任务运行器从入门到实战
前端
smallzip8 分钟前
node大文件拷贝优化(显示进度)
前端·性能优化·node.js
mouseliu9 分钟前
python之二:docker部署项目
前端·python
要加油哦~21 分钟前
css | class中 ‘.‘ 和 ‘:‘ 的使用 | 如,何时用 &.is-selected{ ... } 何时用 &:hover{...}?
前端·css
不浪brown1 小时前
开源!矢量建筑白模泛光特效以及全国77个大中城市的矢量shp数据获取!
前端·cesium
山有木兮木有枝_1 小时前
JavaScript 数据类型与内存分配机制探究
前端
小小小小宇1 小时前
前端 异步任务并发控制
前端
bysking1 小时前
【27-vue3】vue3版本的"指令式弹窗"逻辑函数createModal-bysking
前端·vue.js