清除keep alive缓存

 //   beforeRouteLeave(to, from, next) {  //清除缓存方法二
  //     if (to.path == "/hello") {
  //       this.removeKeepAlive();
  //       next();
  //     }
  //     next();
  //   },
  beforeRouteLeave(to, from, next) {
    //清除缓存方法一
    if (to.path == "/third") {
      if (!from.meta.keepAlive) {
        from.meta.keepAlive = true; //当我们进入到C时开启B的缓存
      }
      next();
    } else {
      from.meta.keepAlive = false;
      //   this.$destroy(); //销毁B的实例
      next(); //当我们前进的不是C时我们让B页面刷新
    }
    console.log("==========");
    console.log("to", to);
    console.log("from", from);
    console.log("==========");
  },
  methods: {
    go() {
      this.$router.push("/third");
      console.log("跳去3");
    }
    // removeKeepAlive() {  //清除缓存方法二,动态删除缓存
    //   var vn = this.$vnode,
    //     p = vn && vn.parent;
    //   if (!vn || !vn.data.keepAlive || !vn.componentOptions) return;
    //   if (!p || !p.componentInstance || !p.componentInstance.cache) return;
    //   var key =
    //     vn.key ||
    //     vn.componentOptions.Ctor.cid +
    //       (vn.componentOptions.tag ? `::${vn.componentOptions.tag}` : "");
    //   var cache = p.componentInstance.cache;
    //   var keys = p.componentInstance.keys;
    //   if (cache[key]) {
    //     if (keys.length) {
    //       var index = keys.indexOf(key);
    //       if (index > -1) {
    //         keys.splice(index, 1);
    //       }
    //     }
    //     delete cache[key];
    //   }
    //   this.$destroy();
    // }
  }
发布了52 篇原创文章 · 获赞 24 · 访问量 1万+

猜你喜欢

转载自blog.csdn.net/weixin_42312074/article/details/105723838