mirror of
https://github.com/m-xlsea/ruoyi-plus-soybean.git
synced 2025-09-24 07:49:47 +08:00
feat(projects): 添加页面缓存、记录在tab中的缓存页面的滚动条位置
This commit is contained in:
1
src/router/helpers/index.ts
Normal file
1
src/router/helpers/index.ts
Normal file
@ -0,0 +1 @@
|
||||
export * from './scroll';
|
33
src/router/helpers/scroll.ts
Normal file
33
src/router/helpers/scroll.ts
Normal file
@ -0,0 +1,33 @@
|
||||
import type { RouterScrollBehavior } from 'vue-router';
|
||||
import { useTabStore } from '@/store';
|
||||
|
||||
export const scrollBehavior: RouterScrollBehavior = (to, from) => {
|
||||
return new Promise(resolve => {
|
||||
const tab = useTabStore();
|
||||
|
||||
if (to.hash) {
|
||||
resolve({
|
||||
el: to.hash,
|
||||
behavior: 'smooth'
|
||||
});
|
||||
}
|
||||
|
||||
const { left, top } = tab.getTabScrollPosition(to.path);
|
||||
const scrollPosition = {
|
||||
left,
|
||||
top
|
||||
};
|
||||
const { scrollLeft, scrollTop } = document.documentElement;
|
||||
|
||||
const isFromCached = Boolean(from.meta.keepAlive);
|
||||
if (isFromCached) {
|
||||
tab.recordTabScrollPosition(from.path, { left: scrollLeft, top: scrollTop });
|
||||
}
|
||||
|
||||
const duration = !scrollPosition.left && !scrollPosition.top ? 0 : 350;
|
||||
|
||||
setTimeout(() => {
|
||||
resolve(scrollPosition);
|
||||
}, duration);
|
||||
});
|
||||
};
|
@ -2,6 +2,7 @@ import type { App } from 'vue';
|
||||
import { createRouter, createWebHistory, createWebHashHistory } from 'vue-router';
|
||||
import { transformAuthRoutesToVueRoutes } from '@/utils';
|
||||
import { constantRoutes } from './routes';
|
||||
import { scrollBehavior } from './helpers';
|
||||
import { createRouterGuard } from './guard';
|
||||
|
||||
const createHistoryFunc = import.meta.env.VITE_IS_VERCEL === '1' ? createWebHashHistory : createWebHistory;
|
||||
@ -9,7 +10,7 @@ const createHistoryFunc = import.meta.env.VITE_IS_VERCEL === '1' ? createWebHash
|
||||
export const router = createRouter({
|
||||
history: createHistoryFunc(import.meta.env.BASE_URL),
|
||||
routes: transformAuthRoutesToVueRoutes(constantRoutes),
|
||||
scrollBehavior: () => ({ left: 0, top: 0 })
|
||||
scrollBehavior
|
||||
});
|
||||
|
||||
export async function setupRouter(app: App) {
|
||||
|
Reference in New Issue
Block a user