修改路由切换初始化滚动条方法,让他显得没那么蠢

This commit is contained in:
ray_wuhao 2023-05-31 17:15:44 +08:00
parent 13b1db79fb
commit d0847ee6f9
6 changed files with 28 additions and 23 deletions

View File

@ -11,5 +11,14 @@
/** vue-router 相关配置入口 */
import type { LayoutInst } from 'naive-ui'
/** 路由切换容器区域 id 配置 */
export const viewScrollContainerId = 'rayLayoutContentWrapperScopeSelector'
/**
*
* ref
*
*/
export const LAYOUT_CONTENT_REF = ref<LayoutInst>()

View File

@ -16,7 +16,10 @@ import ContentWrapper from '@/layout/default/ContentWrapper'
import FooterWrapper from '@/layout/default/FooterWrapper'
import { useSetting, useMenu } from '@/store'
import { viewScrollContainerId } from '@/appConfig/routerConfig'
import {
viewScrollContainerId,
LAYOUT_CONTENT_REF,
} from '@/appConfig/routerConfig'
const Layout = defineComponent({
name: 'Layout',
@ -53,6 +56,7 @@ const Layout = defineComponent({
modelMenuTagSwitch,
cssVarsRef,
isLock,
LAYOUT_CONTENT_REF,
}
},
render() {
@ -68,6 +72,7 @@ const Layout = defineComponent({
<SiderBar />
{this.modelMenuTagSwitch ? <MenuTag /> : ''}
<NLayoutContent
ref="LAYOUT_CONTENT_REF"
class="layout-content__router-view"
nativeScrollbar={false}
{...{ id: viewScrollContainerId }}

View File

@ -8,6 +8,7 @@ interface RouteMeta {
role?: string[]
hidden?: boolean
noLocalTitle?: string | number
ignoreAutoResetScroll?: boolean
}
```
@ -16,8 +17,9 @@ interface RouteMeta {
```
i18nKey: i18n 国际化 key, 会优先使用该字段
icon: icon 图标, 用于 Menu 菜单(依赖 RayIcon 组件实现)
windowOpen: 超链接打开
windowOpen: 超链接打开(新开窗口打开)
role: 权限表
hidden: 是否显示
noLocalTitle: 不使用国际化渲染 Menu Titile
ignoreAutoResetScroll: 该页面内容区域自动初始化滚动条位置
```

View File

@ -9,14 +9,14 @@ export type Component<T = any> =
| (() => Promise<typeof import('*.vue')>)
| (() => Promise<T>)
export interface AppRouteMeta extends IUnknownObjectKey {
export interface AppRouteMeta {
i18nKey?: string
icon?: string
windowOpen?: string
role?: string[]
hidden?: boolean
noLocalTitle?: string | number
ignoreResetScroll?: boolean
ignoreAutoResetScroll?: boolean
}
// @ts-ignore

View File

@ -1,5 +1,4 @@
import { getElement } from '@/utils/element'
import { viewScrollContainerId } from '@/appConfig/routerConfig'
import { LAYOUT_CONTENT_REF } from '@/appConfig/routerConfig'
import type { RouteLocationNormalized } from 'vue-router'
@ -8,28 +7,18 @@ import type { RouteLocationNormalized } from 'vue-router'
* ,
*
* ,
* , meta ignoreResetScroll
*
* , ,
* , meta ignoreAutoResetScroll
*/
const scrollViewToTop = (route: RouteLocationNormalized) => {
const { meta } = route
/** 这个 id 是注入在 layout 中 */
if (!meta?.ignoreResetScroll) {
const scrollViewRoot = getElement(`#${viewScrollContainerId}`)?.[0]
if (scrollViewRoot && typeof scrollViewRoot.scroll) {
/** 找到 NLayoutContent 组件滚动元素 */
const scrollView = scrollViewRoot?.firstElementChild
?.firstChild as HTMLElement
scrollView?.scroll({
top: 0,
left: 0,
behavior: 'smooth',
})
}
if (!meta?.ignoreAutoResetScroll) {
LAYOUT_CONTENT_REF.value?.scrollTo({
top: 0,
left: 0,
behavior: 'smooth',
})
}
}