Vue3手动清理keep-alive组件缓存的方法详解

 更新时间:2024年04月03日 08:59:58   作者:xinfei  
这篇文章主要为大家详细介绍了Vue3中手动清理keep-alive组件缓存的方法,文中的示例代码讲解详细,感兴趣的小伙伴可以跟随小编一起学习一下

Vue3中手动清理keep-alive组件缓存的一个解决方案

源码

  if ((process.env.NODE_ENV !== 'production') || __VUE_PROD_DEVTOOLS__) {
        instance.__v_cache = cache;
    }

    //省略一些代码...

    function pruneCacheEntry(key) {
        const cached = cache.get(key);
        if (!current || cached.type !== current.type) {
            unmount(cached);
        }
        else if (current) {
            // current active instance should no longer be kept-alive.
            // we can't unmount it now but it might be later, so reset its flag now.
            resetShapeFlag(current);
        }
        cache.delete(key);
        keys.delete(key);
    }

这里表明我们有两种修改方案:  

方案一:注释 instance.__v_cache = cache; 这行代码的判断条件,也就是注释掉它的if判断,这样无论在什么环境,我们都可以取到__v_cache对象,这样就可以按照上一篇的方案来解决手动释放的问题

方案二:注意到源码中的pruneCacheEntry函数就是通过key来释放缓存,所以如果仅仅是想通过key来释放缓存,那么可以通过将pruneCacheEntry函数暴露出来实现我们的要求

方案一

修改vue.config.js,在文件开头添加下面的代码:  

    const path = require("path");
    const fs = require("fs");
    try {
      const vue_bundler_file = path.resolve(
        __dirname,
        "node_modules/@vue/runtime-core/dist/runtime-core.esm-bundler.js"
      );
      //使用同步读取文件
      let data = fs.readFileSync(vue_bundler_file, "utf8");
      //如果未添加过
      if (data.indexOf("//__v_cache") < 0) {
        console.log("正在修改源码文件:", vue_bundler_file);
        //先找到__v_cache变量的位置
        let index = data.indexOf("__v_cache");
        if (index >= 0) {
          // 继续往前找if关键字
          index = data.lastIndexOf("if ", index);
          if (index >= 0) {
            //从上一个位置开始
            index -= 1;
            //然后放一个注释
            const comment = " //__v_cache ";
            //然后拼接
            data = data.substring(0, index) + comment + data.substring(index);

            //继续往后找下一个大括号 }
            index = data.indexOf("}", index);
            if (index >= 0) {
              //从上一个位置开始
              index -= 1;
              //然后拼接
              data = data.substring(0, index) + comment + data.substring(index);
            }

            fs.writeFileSync(vue_bundler_file, data, "utf8");
          }
        }
      }
    } catch (er) {
      console.error(er.message);
    }

然后重新启动运行项目,就可以按照上一篇的方式,通过 __v_cache 对象来手动清理keep-alive的缓存了。  

    export default {
      setup() {
        const instance = getCurrentInstance();
        const handler = new KeepAliveHandler();
        onMounted(() => {
          const keepAlive = instance.refs.keepAlive;
          handler.bind(keepAlive);
        });
        const remove = (key) => {
          handler.remove(key);
        };

        return {
          remove,
        };
      },
    };

如果打开 node_modules/@vue/runtime-core/dist/runtime-core.esm-bundler.js 文件,搜索 __v_cache ,会看到这样的代码片段:

方案二

在 vue.config.js 中开头添加如下代码:

    const path = require("path");
    const fs = require("fs");
    try {
      const vue_bundler_file = path.resolve(
        __dirname,
        "node_modules/@vue/runtime-core/dist/runtime-core.esm-bundler.js"
      );
      //使用同步读取文件
      const data = fs.readFileSync(vue_bundler_file, "utf8");
      //如果未添加过
      if (data.indexOf("sharedContext.$pruneCacheEntry") < 0) {
        console.log("正在修改源码文件:", vue_bundler_file);
        //先找到__v_cache变量的位置
        let index = data.indexOf("__v_cache");
        if (index >= 0) {
          // 继续找下一个大括号 }
          index = data.indexOf("}", index);
          if (index >= 0) {
            //从下一个位置开始
            index += 1;
            //然后放一个可以释放的函数
            const remove =
              "        sharedContext.$pruneCacheEntry = (key) => cache.get(key) && pruneCacheEntry(key);";
            //然后拼接
            const result =
              data.substring(0, index) +
              "\r\n" +
              remove +
              "\r\n" +
              data.substring(index);
            fs.writeFileSync(vue_bundler_file, result, "utf8");
          }
        }
      }
    } catch (er) {
      console.error(er.message);
    }
    const path = require("path");
    const fs = require("fs");
    try {
      const vue_bundler_file = path.resolve(
        __dirname,
        "node_modules/@vue/runtime-core/dist/runtime-core.esm-bundler.js"
      );
      //使用同步读取文件
      const data = fs.readFileSync(vue_bundler_file, "utf8");
      //如果未添加过
      if (data.indexOf("sharedContext.$pruneCacheEntry") < 0) {
        console.log("正在修改源码文件:", vue_bundler_file);
        //先找到__v_cache变量的位置
        let index = data.indexOf("__v_cache");
        if (index >= 0) {
          // 继续找下一个大括号 }
          index = data.indexOf("}", index);
          if (index >= 0) {
            //从下一个位置开始
            index += 1;
            //然后放一个可以释放的函数
            const remove =
              "        sharedContext.$pruneCacheEntry = function(key) {\r\n" +
              "            const cached = cache.get(key);\r\n" +
              "            if (cached) {\r\n" +
              "                if (cached.key == current?.key) {\r\n" +
              "                    resetShapeFlag(current);\r\n" +
              "                } else {\r\n" +
              "                    unmount(cached);\r\n" +
              "                }\r\n" +
              "                cache.delete(key);\r\n" +
              "                keys.delete(key);\r\n" +
              "            }\r\n" +
              "        }\r\n"
            //然后拼接
            const result =
              data.substring(0, index) +
              "\r\n" +
              remove +
              "\r\n" +
              data.substring(index);
            fs.writeFileSync(vue_bundler_file, result, "utf8");
          }
        }
      }
    } catch (er) {
      console.error(er.message);
    }

之后,我们项目重新运行后,就可以通过ref取到keep-alive组件的引用,然后使用这个引用对象直接使用$pruneCacheEntry函数来删除指定key的缓存了:  

    this.$refs.keepAlive.$pruneCacheEntry("key")

如果打开 node_modules/@vue/runtime-core/dist/runtime-core.esm-bundler.js 文件,搜索 __v_cache ,会看到这样的代码片段:

结语

目前,目前还没有找到更好的解决方案,我自己采用的是第二种方案,算是暂时解决了问题,当然,两种方案可以结合使用。

到此这篇关于Vue3手动清理keep-alive组件缓存的方法详解的文章就介绍到这了,更多相关Vue3清理keep-alive组件缓存内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

相关文章

  • vue中实现点击变成全屏及缩放功能

    vue中实现点击变成全屏及缩放功能

    这篇文章主要介绍了vue中实现点击变成全屏及缩放功能,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教
    2024-08-08
  • 14个Vue中易被攻击的代码位置小结

    14个Vue中易被攻击的代码位置小结

    在 Vue 框架中,哪些地方容易被攻击呢?写代码的时候要注意什么呢?这篇文章中小编为大家总结了一些常见的容易受到攻击的地方,希望对大家有所帮助
    2024-02-02
  • Vue Element前端应用开发之常规的JS处理函数

    Vue Element前端应用开发之常规的JS处理函数

    在我们使用Vue Element处理界面的时候,往往碰到需要利用JS集合处理的各种方法,如Filter、Map、reduce等方法,也可以涉及到一些对象属性赋值等常规的处理或者递归的处理方法,本篇随笔列出一些在VUE+Element 前端开发中经常碰到的JS处理场景,供参考学习。
    2021-05-05
  • vue2.0父子组件间传递数据的方法

    vue2.0父子组件间传递数据的方法

    本文通过一个小例子给大家介绍了vue2.0父子组件间传递数据的方法,需要的朋友参考下吧
    2018-08-08
  • vite+vue3+element-plus项目搭建的方法步骤

    vite+vue3+element-plus项目搭建的方法步骤

    因为vue3出了一段时间了,element也出了基于vue3.x版本的element-plus,vite打包听说很快,尝试一下,感兴趣的可以了解一下
    2021-06-06
  • 让你30分钟快速掌握vue3教程

    让你30分钟快速掌握vue3教程

    这篇文章主要介绍了让你30分钟快速掌握vue3,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2020-10-10
  • 详解Vue取消eslint语法限制

    详解Vue取消eslint语法限制

    本篇文章给大家分享了Vue取消eslint语法限制的相关知识点内容,有兴趣的朋友们可以参考学习下。
    2018-08-08
  • vue使用v-model进行跨组件绑定的基本实现方法

    vue使用v-model进行跨组件绑定的基本实现方法

    这篇文章主要给大家介绍了关于vue使用v-model进行跨组件绑定的基本实现方法,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2021-04-04
  • Vue与compressor.js实现高效文件压缩的方法

    Vue与compressor.js实现高效文件压缩的方法

    本文将介绍基于 Vue 框架和 compressor.js 的上传时文件压缩实现方法,通过在上传过程中对文件进行压缩,减小文件大小,提升上传速度,为用户创造更快捷、高效的上传体验,感兴趣的朋友跟随小编一起看看吧
    2024-03-03
  • Vue中provide和inject的使用教程详解

    Vue中provide和inject的使用教程详解

    在 Vue 中,provide 和 inject 是用于实现祖先组件向后代组件传递数据的一种方式,本文主要来和大家详细讲讲provide和inject的使用方法,希望对大家有所帮助
    2024-02-02

最新评论