fix: menu scrollbar position cannot be restored after switching routes (#3634)

#### What type of PR is this?

/kind bug
/area console
/milestone 2.4.0

#### What this PR does / why we need it:

修复跳转页面之后无法固定菜单滚动位置的问题。

#### Which issue(s) this PR fixes:

Fixes https://github.com/halo-dev/halo/issues/3633

#### Does this PR introduce a user-facing change?

```release-note
None
```
pull/3625/head^2
Ryan Wang 2023-03-30 11:02:14 +08:00 committed by GitHub
parent 29f3c25a44
commit 0b3343f219
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 13 additions and 19 deletions

View File

@ -18,7 +18,7 @@ import {
useRouter,
type RouteRecordRaw,
} from "vue-router";
import { nextTick, onMounted, onUnmounted, reactive, ref, watch } from "vue";
import { onMounted, onUnmounted, reactive, ref } from "vue";
import axios from "axios";
import GlobalSearchModal from "@/components/global-search/GlobalSearchModal.vue";
import LoginModal from "@/components/login/LoginModal.vue";
@ -28,7 +28,6 @@ import { useRoleStore } from "@/stores/role";
import { hasPermission } from "@/utils/permission";
import { useUserStore } from "@/stores/user";
import { rbacAnnotations } from "@/constants/annotations";
import { useScroll } from "@vueuse/core";
import { defineStore, storeToRefs } from "pinia";
import { useI18n } from "vue-i18n";
import {
@ -189,9 +188,8 @@ const generateMenus = () => {
onMounted(generateMenus);
// store scroll position
// aside scroll
const navbarScroller = ref();
const { y } = useScroll(navbarScroller);
const useNavbarScrollStore = defineStore("navbar", {
state: () => ({
@ -201,20 +199,6 @@ const useNavbarScrollStore = defineStore("navbar", {
const navbarScrollStore = useNavbarScrollStore();
watch(
() => y.value,
() => {
navbarScrollStore.y = y.value;
}
);
onMounted(() => {
nextTick(() => {
y.value = navbarScrollStore.y;
});
});
// aside scroll
const reactiveParams = reactive<UseOverlayScrollbarsParams>({
options: {
scrollbars: {
@ -222,7 +206,17 @@ const reactiveParams = reactive<UseOverlayScrollbarsParams>({
autoHideDelay: 600,
},
},
defer: true,
events: {
scroll: (_, onScrollArgs) => {
const target = onScrollArgs.target as HTMLElement;
navbarScrollStore.y = target.scrollTop;
},
updated: (instance) => {
const { viewport } = instance.elements();
if (!viewport) return;
viewport.scrollTo({ top: navbarScrollStore.y });
},
},
});
const [initialize] = useOverlayScrollbars(reactiveParams);
onMounted(() => {