Skip to content

Commit

Permalink
[+] 路由时自动滚动到顶部
Browse files Browse the repository at this point in the history
  • Loading branch information
Muska-Ami committed Oct 11, 2024
1 parent d0b3c9a commit 96b7401
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 13 deletions.
12 changes: 9 additions & 3 deletions src/App.vue
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
<guest-sidebar v-if="showGuestSidebar" />
<main-sidebar v-if="showMainSidebar" />
</div>
<n-layout :native-scrollbar="false">
<n-layout-content ref="contentRef" :native-scrollbar="false">
<div class="content">
<router-view v-slot="{ Component }">
<keep-alive :max="10">
Expand All @@ -35,7 +35,7 @@
</div>
<n-divider />
<the-footer />
</n-layout>
</n-layout-content>
</n-layout>
</n-layout>
</n-notification-provider>
Expand Down Expand Up @@ -74,7 +74,7 @@ import api from '@/api'
import { sendWarningMessage } from '@/utils/message'
import { logout } from '@/utils/profile'
import router from '@router'
import { useRoute } from 'vue-router'
import { useRoute, useRouter } from 'vue-router'
// 手机状态下收缩菜单栏
const collapsed = ref(true)
Expand All @@ -84,17 +84,23 @@ if (document.body.clientWidth >= 1000) {
const osThemeRef = useOsTheme()
const route = useRoute()
const vRouter = useRouter()
const theme = computed(() => (osThemeRef.value === 'dark' ? darkTheme : null))
// let initFinished = false
const loading = ref(true)
const tokenValid = ref(false)
const showGuestSidebar = ref(false)
const showMainSidebar = ref(false)
const contentRef = ref(null)
hljs.registerLanguage('ini', ini)
hljs.registerLanguage('nginx', nginx)
vRouter.afterEach(() => {
contentRef.value?.scrollTo({ top: 0, behavior: 'smooth' })
})
// function getMessage(e) {
// const rs = JSON.parse(e.data)
// // 通知
Expand Down
2 changes: 1 addition & 1 deletion src/components/sidebar/GuestSidebar.vue
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
@expand="collapsed = false"
collapse-mode="width"
:collapsed-width="64"
:native-scrollbar="true"
:native-scrollbar="false"
id="sider"
style="height: 100%; bottom: 0"
>
Expand Down
3 changes: 1 addition & 2 deletions src/components/sidebar/MainSidebar.vue
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,7 @@
@expand="collapsed = false"
collapse-mode="width"
:collapsed-width="64"
:native-scrollbar="true"
:inverted="false"
:native-scrollbar="false"
id="sider"
style="height: 100%; bottom: 0"
>
Expand Down
8 changes: 1 addition & 7 deletions src/router/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -284,13 +284,7 @@ const routes = [

const router = createRouter({
history: createWebHistory(),
routes,
scrollBehavior(to, from, savedPosition) {
return {
top: 0,
behavior: 'smooth'
}
}
routes
})

// // 检查本地存储是否存在token,若存在则直接使用
Expand Down

0 comments on commit 96b7401

Please sign in to comment.