mirror of
https://github.com/jxxghp/MoviePilot-Frontend.git
synced 2026-09-05 23:56:42 +08:00
fix router
This commit is contained in:
+1
-33
@@ -2,7 +2,6 @@ import { createApp } from 'vue'
|
|||||||
import '@/@iconify/icons-bundle'
|
import '@/@iconify/icons-bundle'
|
||||||
import ToastPlugin from 'vue-toast-notification'
|
import ToastPlugin from 'vue-toast-notification'
|
||||||
import VuetifyUseDialog from 'vuetify-use-dialog'
|
import VuetifyUseDialog from 'vuetify-use-dialog'
|
||||||
import { configureNProgress, doneNProgress, startNProgress } from '@/api/nprogress'
|
|
||||||
import App from '@/App.vue'
|
import App from '@/App.vue'
|
||||||
import vuetify from '@/plugins/vuetify'
|
import vuetify from '@/plugins/vuetify'
|
||||||
import { loadFonts } from '@/plugins/webfontloader'
|
import { loadFonts } from '@/plugins/webfontloader'
|
||||||
@@ -15,9 +14,6 @@ import 'vue-toast-notification/dist/theme-default.css'
|
|||||||
|
|
||||||
loadFonts()
|
loadFonts()
|
||||||
|
|
||||||
// Nprogress
|
|
||||||
configureNProgress()
|
|
||||||
|
|
||||||
// Create vue app
|
// Create vue app
|
||||||
const app = createApp(App)
|
const app = createApp(App)
|
||||||
|
|
||||||
@@ -28,32 +24,4 @@ app.use(vuetify)
|
|||||||
.use(ToastPlugin, {
|
.use(ToastPlugin, {
|
||||||
position: 'bottom-right',
|
position: 'bottom-right',
|
||||||
})
|
})
|
||||||
.use(VuetifyUseDialog)
|
.use(VuetifyUseDialog).mount('#app')
|
||||||
|
|
||||||
// 路由导航守卫
|
|
||||||
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)
|
|
||||||
}
|
|
||||||
})
|
|
||||||
|
|||||||
+23
-1
@@ -1,12 +1,17 @@
|
|||||||
import { createRouter, createWebHistory } from 'vue-router'
|
import { createRouter, createWebHistory } from 'vue-router'
|
||||||
|
import { configureNProgress, doneNProgress, startNProgress } from '@/api/nprogress'
|
||||||
|
import store from '@/store'
|
||||||
|
|
||||||
|
// Nprogress
|
||||||
|
configureNProgress()
|
||||||
|
|
||||||
|
// Router
|
||||||
const router = createRouter({
|
const router = createRouter({
|
||||||
history: createWebHistory(import.meta.env.BASE_URL),
|
history: createWebHistory(import.meta.env.BASE_URL),
|
||||||
scrollBehavior(to, from, savedPosition) {
|
scrollBehavior(to, from, savedPosition) {
|
||||||
// 如果页面有缓存那么恢复其位置, 否则始终滚动到顶部
|
// 如果页面有缓存那么恢复其位置, 否则始终滚动到顶部
|
||||||
if (to.meta.keepAlive && savedPosition)
|
if (to.meta.keepAlive && savedPosition)
|
||||||
return savedPosition
|
return savedPosition
|
||||||
// console.log('top: 0')
|
|
||||||
return { top: 0 }
|
return { top: 0 }
|
||||||
},
|
},
|
||||||
routes: [
|
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
|
export default router
|
||||||
|
|||||||
Reference in New Issue
Block a user