feat(projects): 添加页面缓存、记录在tab中的缓存页面的滚动条位置

This commit is contained in:
Soybean
2022-01-21 23:59:14 +08:00
parent db3c25ea14
commit 1d63a83822
26 changed files with 343 additions and 160 deletions

35
src/utils/router/cache.ts Normal file
View File

@ -0,0 +1,35 @@
import type { RouteRecordRaw } from 'vue-router';
/**
* 获取缓存的路由对应组件的名称
* @param routes - 转换后的vue路由
*/
export function getCacheRoutes(routes: RouteRecordRaw[]) {
const cacheNames: string[] = [];
routes.forEach(route => {
// 只需要获取二级路由的缓存的组件名
if (hasChildren(route)) {
route.children!.forEach(item => {
if (isKeepAlive(item)) {
cacheNames.push(item.name as string);
}
});
}
});
return cacheNames;
}
/**
* 路由是否缓存
* @param route
*/
function isKeepAlive(route: RouteRecordRaw) {
return Boolean(route?.meta?.keepAlive);
}
/**
* 是否有二级路由
* @param route
*/
function hasChildren(route: RouteRecordRaw) {
return Boolean(route.children && route.children.length);
}