fix router

This commit is contained in:
jxxghp
2023-09-19 18:02:44 +08:00
parent 4f625291a5
commit 2e987701a8
2 changed files with 24 additions and 34 deletions

View File

@@ -2,7 +2,6 @@ import { createApp } from 'vue'
import '@/@iconify/icons-bundle'
import ToastPlugin from 'vue-toast-notification'
import VuetifyUseDialog from 'vuetify-use-dialog'
import { configureNProgress, doneNProgress, startNProgress } from '@/api/nprogress'
import App from '@/App.vue'
import vuetify from '@/plugins/vuetify'
import { loadFonts } from '@/plugins/webfontloader'
@@ -15,9 +14,6 @@ import 'vue-toast-notification/dist/theme-default.css'
loadFonts()
// Nprogress
configureNProgress()
// Create vue app
const app = createApp(App)
@@ -28,32 +24,4 @@ app.use(vuetify)
.use(ToastPlugin, {
position: 'bottom-right',
})
.use(VuetifyUseDialog)
// 路由导航守卫
router.beforeEach((to, from, next) => {
const isAuthenticated = store.state.auth.token !== null
if (to.meta.requiresAuth && !isAuthenticated) {
next('/login')
}
else {
startNProgress()
next()
}
})
// 记录首次开启app
let isFirstApp: Boolean = true
router.afterEach(() => {
doneNProgress()
if (isFirstApp) {
// 标记首次载入完成, 再绑定app
isFirstApp = false
// 先不加延迟试试
app.mount('#app')
// setTimeout(() => {
// app.mount('#app')
// }, 300)
}
})
.use(VuetifyUseDialog).mount('#app')

View File

@@ -1,12 +1,17 @@
import { createRouter, createWebHistory } from 'vue-router'
import { configureNProgress, doneNProgress, startNProgress } from '@/api/nprogress'
import store from '@/store'
// Nprogress
configureNProgress()
// Router
const router = createRouter({
history: createWebHistory(import.meta.env.BASE_URL),
scrollBehavior(to, from, savedPosition) {
// 如果页面有缓存那么恢复其位置, 否则始终滚动到顶部
if (to.meta.keepAlive && savedPosition)
return savedPosition
// console.log('top: 0')
return { top: 0 }
},
routes: [
@@ -150,4 +155,21 @@ const router = createRouter({
],
})
// 路由导航守卫
router.beforeEach((to, from, next) => {
const isAuthenticated = store.state.auth.token !== null
if (to.meta.requiresAuth && !isAuthenticated) {
next('/login')
}
else {
startNProgress()
next()
}
})
router.afterEach(() => {
doneNProgress()
})
export default router