61 lines
1.6 KiB
TypeScript
61 lines
1.6 KiB
TypeScript
/**
|
|
* router/index.ts
|
|
*
|
|
* Automatic routes for `./src/pages/*.vue`
|
|
*/
|
|
|
|
import { setupLayouts } from 'virtual:generated-layouts'
|
|
// Composables
|
|
import { createRouter, createWebHistory } from 'vue-router/auto'
|
|
import { routes } from 'vue-router/auto-routes'
|
|
|
|
const router = createRouter({
|
|
history: createWebHistory(import.meta.env.BASE_URL),
|
|
routes: setupLayouts(routes),
|
|
})
|
|
|
|
// Workaround for https://github.com/vitejs/vite/issues/11804
|
|
router.onError((err, to) => {
|
|
if (err?.message?.includes?.('Failed to fetch dynamically imported module')) {
|
|
if (localStorage.getItem('vuetify:dynamic-reload')) {
|
|
console.error('Dynamic import error, reloading page did not fix it', err)
|
|
} else {
|
|
console.log('Reloading page to fix dynamic import error')
|
|
localStorage.setItem('vuetify:dynamic-reload', 'true')
|
|
location.assign(to.fullPath)
|
|
}
|
|
} else {
|
|
console.error(err)
|
|
}
|
|
})
|
|
|
|
router.isReady().then(() => {
|
|
localStorage.removeItem('vuetify:dynamic-reload')
|
|
})
|
|
|
|
router.beforeEach((to, from, next) => {
|
|
// 如果访问根路径且不是直接输入地址访问
|
|
if (to.path === '/' && from.path !== '/') {
|
|
// 检查是否有保存的路径
|
|
const lastRoute = sessionStorage.getItem('lastRoute')
|
|
const savedPath = localStorage.getItem('lastPath')
|
|
|
|
if (lastRoute || savedPath) {
|
|
// 重定向到上次访问的页面
|
|
next(lastRoute || savedPath || '/calculators/paper-tube-weight')
|
|
return
|
|
}
|
|
}
|
|
|
|
// 保存当前路由(非首页)
|
|
if (to.path !== '/') {
|
|
sessionStorage.setItem('lastRoute', to.fullPath)
|
|
localStorage.setItem('lastPath', to.fullPath)
|
|
}
|
|
|
|
next()
|
|
|
|
})
|
|
|
|
export default router
|