最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Vue3手動(dòng)清理keep-alive組件緩存的方法詳解

 更新時(shí)間:2024年04月03日 08:59:58   作者:xinfei  
這篇文章主要為大家詳細(xì)介紹了Vue3中手動(dòng)清理keep-alive組件緩存的方法,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下

Vue3中手動(dòng)清理keep-alive組件緩存的一個(gè)解決方案

源碼

  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判斷,這樣無(wú)論在什么環(huán)境,我們都可以取到__v_cache對(duì)象,這樣就可以按照上一篇的方案來(lái)解決手動(dòng)釋放的問(wèn)題

方案二:注意到源碼中的pruneCacheEntry函數(shù)就是通過(guò)key來(lái)釋放緩存,所以如果僅僅是想通過(guò)key來(lái)釋放緩存,那么可以通過(guò)將pruneCacheEntry函數(shù)暴露出來(lái)實(shí)現(xiàn)我們的要求

方案一

修改vue.config.js,在文件開(kāi)頭添加下面的代碼:  

    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");
      //如果未添加過(guò)
      if (data.indexOf("http://__v_cache") < 0) {
        console.log("正在修改源碼文件:", vue_bundler_file);
        //先找到__v_cache變量的位置
        let index = data.indexOf("__v_cache");
        if (index >= 0) {
          // 繼續(xù)往前找if關(guān)鍵字
          index = data.lastIndexOf("if ", index);
          if (index >= 0) {
            //從上一個(gè)位置開(kāi)始
            index -= 1;
            //然后放一個(gè)注釋
            const comment = " //__v_cache ";
            //然后拼接
            data = data.substring(0, index) + comment + data.substring(index);

            //繼續(xù)往后找下一個(gè)大括號(hào) }
            index = data.indexOf("}", index);
            if (index >= 0) {
              //從上一個(gè)位置開(kāi)始
              index -= 1;
              //然后拼接
              data = data.substring(0, index) + comment + data.substring(index);
            }

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

然后重新啟動(dòng)運(yùn)行項(xiàng)目,就可以按照上一篇的方式,通過(guò) __v_cache 對(duì)象來(lái)手動(dòng)清理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,
        };
      },
    };

如果打開(kāi) node_modules/@vue/runtime-core/dist/runtime-core.esm-bundler.js 文件,搜索 __v_cache ,會(huì)看到這樣的代碼片段:

方案二

在 vue.config.js 中開(kāi)頭添加如下代碼:

    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");
      //如果未添加過(guò)
      if (data.indexOf("sharedContext.$pruneCacheEntry") < 0) {
        console.log("正在修改源碼文件:", vue_bundler_file);
        //先找到__v_cache變量的位置
        let index = data.indexOf("__v_cache");
        if (index >= 0) {
          // 繼續(xù)找下一個(gè)大括號(hào) }
          index = data.indexOf("}", index);
          if (index >= 0) {
            //從下一個(gè)位置開(kāi)始
            index += 1;
            //然后放一個(gè)可以釋放的函數(shù)
            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");
      //如果未添加過(guò)
      if (data.indexOf("sharedContext.$pruneCacheEntry") < 0) {
        console.log("正在修改源碼文件:", vue_bundler_file);
        //先找到__v_cache變量的位置
        let index = data.indexOf("__v_cache");
        if (index >= 0) {
          // 繼續(xù)找下一個(gè)大括號(hào) }
          index = data.indexOf("}", index);
          if (index >= 0) {
            //從下一個(gè)位置開(kāi)始
            index += 1;
            //然后放一個(gè)可以釋放的函數(shù)
            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);
    }

之后,我們項(xiàng)目重新運(yùn)行后,就可以通過(guò)ref取到keep-alive組件的引用,然后使用這個(gè)引用對(duì)象直接使用$pruneCacheEntry函數(shù)來(lái)刪除指定key的緩存了:  

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

如果打開(kāi) node_modules/@vue/runtime-core/dist/runtime-core.esm-bundler.js 文件,搜索 __v_cache ,會(huì)看到這樣的代碼片段:

結(jié)語(yǔ)

目前,目前還沒(méi)有找到更好的解決方案,我自己采用的是第二種方案,算是暫時(shí)解決了問(wèn)題,當(dāng)然,兩種方案可以結(jié)合使用。

到此這篇關(guān)于Vue3手動(dòng)清理keep-alive組件緩存的方法詳解的文章就介紹到這了,更多相關(guān)Vue3清理keep-alive組件緩存內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評(píng)論

通化县| 南城县| 隆回县| 游戏| 苏州市| 岳池县| 雷波县| 荣昌县| 施甸县| 丰都县| 湖口县| 宜宾市| 弋阳县| 彰武县| 台南市| 威信县| 南川市| 和顺县| 河西区| 驻马店市| 宽甸| 西乡县| 眉山市| 合肥市| 开远市| 鄂伦春自治旗| 舒城县| 桐庐县| 马关县| 呼伦贝尔市| 建昌县| 上栗县| 和田市| 张北县| 滕州市| 华池县| 固安县| 三门县| 山东省| 南澳县| 博客|