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判斷,這樣無論在什么環(huán)境,我們都可以取到__v_cache對象,這樣就可以按照上一篇的方案來解決手動釋放的問題
方案二:注意到源碼中的pruneCacheEntry函數(shù)就是通過key來釋放緩存,所以如果僅僅是想通過key來釋放緩存,那么可以通過將pruneCacheEntry函數(shù)暴露出來實現(xiàn)我們的要求
方案一
修改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("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) {
//從上一個位置開始
index -= 1;
//然后放一個注釋
const comment = " //__v_cache ";
//然后拼接
data = data.substring(0, index) + comment + data.substring(index);
//繼續(xù)往后找下一個大括號 }
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) {
// 繼續(xù)找下一個大括號 }
index = data.indexOf("}", index);
if (index >= 0) {
//從下一個位置開始
index += 1;
//然后放一個可以釋放的函數(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");
//如果未添加過
if (data.indexOf("sharedContext.$pruneCacheEntry") < 0) {
console.log("正在修改源碼文件:", vue_bundler_file);
//先找到__v_cache變量的位置
let index = data.indexOf("__v_cache");
if (index >= 0) {
// 繼續(xù)找下一個大括號 }
index = data.indexOf("}", index);
if (index >= 0) {
//從下一個位置開始
index += 1;
//然后放一個可以釋放的函數(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);
}
之后,我們項目重新運行后,就可以通過ref取到keep-alive組件的引用,然后使用這個引用對象直接使用$pruneCacheEntry函數(shù)來刪除指定key的緩存了:
this.$refs.keepAlive.$pruneCacheEntry("key")如果打開 node_modules/@vue/runtime-core/dist/runtime-core.esm-bundler.js 文件,搜索 __v_cache ,會看到這樣的代碼片段:

結(jié)語
目前,目前還沒有找到更好的解決方案,我自己采用的是第二種方案,算是暫時解決了問題,當然,兩種方案可以結(jié)合使用。
到此這篇關(guān)于Vue3手動清理keep-alive組件緩存的方法詳解的文章就介紹到這了,更多相關(guān)Vue3清理keep-alive組件緩存內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Vue Element前端應用開發(fā)之常規(guī)的JS處理函數(shù)
在我們使用Vue Element處理界面的時候,往往碰到需要利用JS集合處理的各種方法,如Filter、Map、reduce等方法,也可以涉及到一些對象屬性賦值等常規(guī)的處理或者遞歸的處理方法,本篇隨筆列出一些在VUE+Element 前端開發(fā)中經(jīng)常碰到的JS處理場景,供參考學習。2021-05-05
vite+vue3+element-plus項目搭建的方法步驟
因為vue3出了一段時間了,element也出了基于vue3.x版本的element-plus,vite打包聽說很快,嘗試一下,感興趣的可以了解一下2021-06-06
vue使用v-model進行跨組件綁定的基本實現(xiàn)方法
這篇文章主要給大家介紹了關(guān)于vue使用v-model進行跨組件綁定的基本實現(xiàn)方法,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2021-04-04
Vue與compressor.js實現(xiàn)高效文件壓縮的方法
本文將介紹基于 Vue 框架和 compressor.js 的上傳時文件壓縮實現(xiàn)方法,通過在上傳過程中對文件進行壓縮,減小文件大小,提升上傳速度,為用戶創(chuàng)造更快捷、高效的上傳體驗,感興趣的朋友跟隨小編一起看看吧2024-03-03

