2021-04-21 19:56:30 +08:00
|
|
|
import { nextTick } from 'vue'
|
2020-09-08 22:04:45 +08:00
|
|
|
import { createRouter, createWebHistory } from 'vue-router'
|
2020-03-20 17:32:05 +08:00
|
|
|
|
2020-09-27 22:27:25 +08:00
|
|
|
export const loadingBarApiRef = {}
|
|
|
|
|
2020-09-08 22:04:45 +08:00
|
|
|
export default function createDemoRouter (app, routes) {
|
|
|
|
const router = createRouter({
|
|
|
|
history: createWebHistory(),
|
2020-03-20 17:32:05 +08:00
|
|
|
routes
|
|
|
|
})
|
|
|
|
|
2020-09-27 22:27:25 +08:00
|
|
|
router.beforeEach(function (to, from, next) {
|
2020-10-06 23:23:36 +08:00
|
|
|
if (!from || to.path !== from.path) {
|
|
|
|
if (loadingBarApiRef.value) {
|
|
|
|
loadingBarApiRef.value.start()
|
|
|
|
}
|
2020-09-27 22:27:25 +08:00
|
|
|
}
|
|
|
|
next()
|
|
|
|
})
|
|
|
|
|
|
|
|
router.afterEach(function (to, from) {
|
2020-10-06 23:23:36 +08:00
|
|
|
if (!from || to.path !== from.path) {
|
2020-09-27 22:27:25 +08:00
|
|
|
if (loadingBarApiRef.value) {
|
|
|
|
loadingBarApiRef.value.finish()
|
|
|
|
}
|
2021-05-18 13:54:58 +08:00
|
|
|
if (to.hash && to.hash !== from.hash) {
|
2021-04-21 19:56:30 +08:00
|
|
|
nextTick(() => {
|
|
|
|
const el = document.querySelector(to.hash)
|
|
|
|
if (el) el.scrollIntoView()
|
|
|
|
})
|
|
|
|
}
|
2020-09-27 22:27:25 +08:00
|
|
|
}
|
|
|
|
})
|
2020-09-08 22:04:45 +08:00
|
|
|
|
2020-03-20 17:32:05 +08:00
|
|
|
return router
|
|
|
|
}
|