Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
Dr.Disrespect
vue-vben-admin
提交
fef36440
V
vue-vben-admin
项目概览
Dr.Disrespect
/
vue-vben-admin
与 Fork 源项目一致
从无法访问的项目Fork
通知
1
Star
0
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
DevOps
流水线
流水线任务
计划
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
V
vue-vben-admin
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
DevOps
DevOps
流水线
流水线任务
计划
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
流水线任务
提交
Issue看板
体验新版 GitCode,发现更多精彩内容 >>
提交
fef36440
编写于
12月 04, 2020
作者:
V
vben
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
fix: page switching did not return to the top
上级
f4621cc6
变更
3
显示空白变更内容
内联
并排
Showing
3 changed file
with
42 addition
and
30 deletion
+42
-30
src/router/guard/index.ts
src/router/guard/index.ts
+12
-1
src/router/index.ts
src/router/index.ts
+3
-1
src/router/scrollBehaviour.ts
src/router/scrollBehaviour.ts
+27
-28
未找到文件。
src/router/guard/index.ts
浏览文件 @
fef36440
import
{
isNavigationFailure
,
Router
}
from
'
vue-router
'
;
import
{
isNavigationFailure
,
Route
LocationNormalized
,
Route
r
}
from
'
vue-router
'
;
import
{
Modal
,
notification
}
from
'
ant-design-vue
'
;
...
...
@@ -19,6 +19,12 @@ import { REDIRECT_NAME } from '/@/router/constant';
const
{
closeMessageOnSwitch
,
removeAllHttpPending
}
=
useProjectSetting
();
const
globSetting
=
useGlobSetting
();
const
body
=
document
.
body
;
const
isHash
=
(
href
:
string
)
=>
{
return
/^#/
.
test
(
href
);
};
export
function
createGuard
(
router
:
Router
)
{
let
axiosCanceler
:
Nullable
<
AxiosCanceler
>
;
if
(
removeAllHttpPending
)
{
...
...
@@ -45,8 +51,13 @@ export function createGuard(router: Router) {
});
router
.
afterEach
((
to
,
from
,
failure
)
=>
{
// scroll top
isHash
((
to
as
RouteLocationNormalized
&
{
href
:
string
})?.
href
)
&&
body
.
scrollTo
(
0
,
0
);
loadedPageMap
.
set
(
to
.
path
,
true
);
const
{
t
}
=
useI18n
();
// change html title
to
.
name
!==
REDIRECT_NAME
&&
setTitle
(
t
(
to
.
meta
.
title
),
globSetting
.
title
);
...
...
src/router/index.ts
浏览文件 @
fef36440
...
...
@@ -8,9 +8,11 @@ import { createGuard } from './guard/';
import
{
basicRoutes
}
from
'
./routes/
'
;
import
{
scrollBehavior
}
from
'
./scrollBehaviour
'
;
export
const
hashRouter
=
createWebHashHistory
();
// app router
const
router
=
createRouter
({
history
:
createWebHashHistory
()
,
history
:
hashRouter
,
routes
:
basicRoutes
as
RouteRecordRaw
[],
strict
:
true
,
scrollBehavior
:
scrollBehavior
,
...
...
src/router/scrollBehaviour.ts
浏览文件 @
fef36440
// see https://github.com/vuejs/vue-router-next/blob/master/playground/scrollWaiter.ts
import
type
{
RouteLocationNormalized
}
from
'
vue-router
'
;
// class ScrollQueue {
// private resolve: (() => void) | null = null;
// private promise: Promise<any> | null = null;
// add() {
// this.promise = new Promise((resolve) => {
// this.resolve = resolve as () => void;
// });
// }
// flush() {
// this.resolve && this.resolve();
// this.resolve = null;
// this.promise = null;
// }
// async wait() {
// await this.promise;
// }
// }
// const scrollWaiter = new ScrollQueue();
/**
* Handles the scroll behavior on route navigation
*
...
...
@@ -8,10 +32,9 @@
*/
// @ts-ignore
export
async
function
scrollBehavior
(
to
,
from
,
savedPosition
)
{
await
scrollWaiter
.
wait
();
//
await scrollWaiter.wait();
// Use predefined scroll behavior if defined, defaults to no scroll behavior
const
behavior
=
document
.
documentElement
.
style
.
scrollBehavior
||
'
auto
'
;
const
behavior
=
'
smooth
'
;
// Returning the `savedPosition` (if available) will result in a native-like
// behavior when navigating with back/forward buttons
if
(
savedPosition
)
{
...
...
@@ -24,7 +47,7 @@ export async function scrollBehavior(to, from, savedPosition) {
}
// Check if any matched route config has meta that discourages scrolling to top
if
(
to
.
matched
.
some
((
m
:
any
)
=>
m
.
meta
.
scrollToTop
===
false
))
{
if
(
to
.
matched
.
some
((
m
:
RouteLocationNormalized
)
=>
m
.
meta
.
scrollToTop
===
false
))
{
// Leave scroll as it is
return
false
;
}
...
...
@@ -32,27 +55,3 @@ export async function scrollBehavior(to, from, savedPosition) {
// Always scroll to top
return
{
left
:
0
,
top
:
0
,
behavior
};
}
// see https://github.com/vuejs/vue-router-next/blob/master/playground/scrollWaiter.ts
class
ScrollQueue
{
private
resolve
:
(()
=>
void
)
|
null
=
null
;
private
promise
:
Promise
<
any
>
|
null
=
null
;
add
()
{
this
.
promise
=
new
Promise
((
resolve
)
=>
{
this
.
resolve
=
resolve
as
()
=>
void
;
});
}
flush
()
{
this
.
resolve
&&
this
.
resolve
();
this
.
resolve
=
null
;
this
.
promise
=
null
;
}
async
wait
()
{
await
this
.
promise
;
}
}
export
const
scrollWaiter
=
new
ScrollQueue
();
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录