暴力解决keep alive 多级组件缓存失败的问题

931 阅读1分钟

前言:项目基于vue-element-admin二次开发 这周被产品组拉来填坑,项目已经进入测试阶段,后台管理系统,涉及到多级路由,测试的时候发现,二级路由keep alive:include能正常缓存组件,but麻烦的事情来了,切换到三级和多级路由后的页面上,并不能正常缓存组件 ,查了很多资料,也看了官方的描述,然而问题并没有得到解决

有一种方法提供了我思路   beforeRouteLeave,路由离开之前找到组件对应的cache值,后来想到,在关闭这个组件和跳转到其他组件的时候都会触发这个事件,引申到elementUI的页签,在关闭页签的时候去找到当前组件的cache和key值 在集合里面删掉即可

上一段代码,

elementUI的页签还有关闭其他组件和关闭所有组件

代码和这个类似  只是判断不同,关闭全部的时候直接把cache 和 keys 清空即可

//关闭当前页面  清除当前页面的keep alive的缓存    clearKeepAliveCache(that, view) {      var vnode = null;      for (let i = 0; i < that.$parent.$children.length; i++) {        if (that.$parent.$children[i].$vnode.tag.indexOf("-AppMain") > -1) {          let isSet = false;          if (that.$parent.$children[i].$vnode.componentInstance) {            for (              let j = 0;              j <              that.$parent.$children[i].$vnode.componentInstance.$children                .length;              j++            ) {              if (                that.$parent.$children[i].$vnode.componentInstance.$children[                  j                ].$vnode.tag.indexOf("-breadcrumb") < 0              ) {                vnode =                  that.$parent.$children[i].$vnode.componentInstance.$children[                    j                  ].$vnode.componentInstance.$vnode.parent;                isSet = true;                break;              }            }          }          if (isSet) {            break;          }        }      }      if (vnode) {        for (let i = 0; i < vnode.componentInstance.keys.length; i++) {          let arr = vnode.componentInstance.keys[i].split("-");          let path = arr[arr.length - 1];          if (path === view.path) {            delete vnode.componentInstance.cache[              vnode.componentInstance.keys[i]            ];            vnode.componentInstance.keys.splice(i, 1);            break;          }        }      }    }