-
Notifications
You must be signed in to change notification settings - Fork 267
/
Copy pathindex.ts
45 lines (32 loc) · 1.11 KB
/
index.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
import { createRouter, createWebHistory } from 'vue-router/auto'
import { handleHotUpdate, routes } from 'vue-router/auto-routes'
import NProgress from 'nprogress'
import 'nprogress/nprogress.css'
import type { EnhancedRouteLocation } from './types'
import useRouteCacheStore from '@/stores/modules/routeCache'
import { useUserStore } from '@/stores'
import { isLogin } from '@/utils/auth'
import setPageTitle from '@/utils/set-page-title'
NProgress.configure({ showSpinner: true, parent: '#app' })
const router = createRouter({
history: createWebHistory(import.meta.env.VITE_APP_PUBLIC_PATH),
routes,
})
// This will update routes at runtime without reloading the page
if (import.meta.hot)
handleHotUpdate(router)
router.beforeEach(async (to: EnhancedRouteLocation) => {
NProgress.start()
const routeCacheStore = useRouteCacheStore()
const userStore = useUserStore()
// Route cache
routeCacheStore.addRoute(to)
// Set page title
setPageTitle(to.meta.title)
if (isLogin() && !userStore.userInfo?.uid)
await userStore.info()
})
router.afterEach(() => {
NProgress.done()
})
export default router