vite-vue3-lowcode/preview/App.vue

53 lines
1.4 KiB
Vue
Raw Normal View History

2021-05-06 00:35:05 +08:00
<template>
2021-05-06 20:10:19 +08:00
<router-view #="{ Component, route }">
<keep-alive ref="keepAliveRef">
<component :is="Component" :key="route.path" />
</keep-alive>
2021-05-06 20:10:19 +08:00
</router-view>
2021-05-06 00:35:05 +08:00
</template>
<script lang="ts">
2022-07-03 02:38:58 +08:00
import { defineComponent, ref, watch } from 'vue';
import { useRoute } from 'vue-router';
import { CacheEnum } from '@/enums';
import { VisualEditorModelValue } from '@/visual-editor/visual-editor.utils';
2021-05-06 20:10:19 +08:00
2022-07-03 02:38:58 +08:00
export default defineComponent({
name: 'App',
setup() {
const keepAliveRef = ref();
const route = useRoute();
const jsonData: VisualEditorModelValue = JSON.parse(
localStorage.getItem(CacheEnum.PAGE_DATA_KEY) as string,
);
2021-07-14 10:35:54 +08:00
2022-07-03 02:38:58 +08:00
// 不需要缓存的页面
const notNeedcachePages = Object.keys(jsonData.pages).filter(
(key) => !jsonData.pages[key].config.keepAlive,
);
console.log('notNeedcachePages:', notNeedcachePages);
2022-07-03 02:38:58 +08:00
watch(
() => route.path,
(path) => {
if (notNeedcachePages.includes(path)) {
// 获取keep-alive缓存
const routeCaches = keepAliveRef.value?.$?.__v_cache;
console.log('keep-alive cache', path, routeCaches);
// 从keep-alive的缓存中删除不需要缓存的路由
routeCaches.delete(path);
}
},
);
2022-07-03 02:38:58 +08:00
return { keepAliveRef };
},
});
2021-05-06 00:35:05 +08:00
</script>
<style>
2022-07-03 02:38:58 +08:00
body::-webkit-scrollbar {
width: 0;
}
2021-05-06 00:35:05 +08:00
</style>