大屏项目初始化
This commit is contained in:
63
screen-vue/src/router/index.js
Normal file
63
screen-vue/src/router/index.js
Normal file
@@ -0,0 +1,63 @@
|
||||
import { createRouter, createWebHistory } from 'vue-router'
|
||||
import Login from '@/views/Login.vue'
|
||||
import Dashboard from '@/views/desktop/index.vue'
|
||||
|
||||
// 路由规则
|
||||
const routes = [
|
||||
{
|
||||
path: '/',
|
||||
redirect: '/login' // 默认跳登录页
|
||||
},
|
||||
{
|
||||
path: '/login',
|
||||
name: 'Login',
|
||||
component: Login,
|
||||
meta: {
|
||||
isPublic: true // 标记为公开页面,无需登录
|
||||
}
|
||||
},
|
||||
{
|
||||
path: '/dashboard',
|
||||
name: 'Dashboard',
|
||||
component: Dashboard,
|
||||
meta: {
|
||||
requiresAuth: true // 需要登录验证
|
||||
}
|
||||
},
|
||||
// 关键补充:404兜底路由(匹配所有未定义的前端路由)
|
||||
{
|
||||
path: '/:pathMatch(.*)*',
|
||||
redirect: '/login' // 未知路径重定向到登录页
|
||||
}
|
||||
]
|
||||
|
||||
const router = createRouter({
|
||||
history: createWebHistory(import.meta.env.BASE_URL || '/'),
|
||||
routes,
|
||||
scrollBehavior() {
|
||||
return { top: 0 }
|
||||
}
|
||||
})
|
||||
|
||||
// 路由守卫:验证登录状态(优化版,增加容错)
|
||||
router.beforeEach((to, from, next) => {
|
||||
// 公开页面直接放行
|
||||
if (to.meta.isPublic) {
|
||||
next()
|
||||
return
|
||||
}
|
||||
|
||||
// 非公开页面校验token
|
||||
try {
|
||||
const token = localStorage.getItem('token')
|
||||
if (token && token.trim() !== '') {
|
||||
next()
|
||||
} else {
|
||||
next({ path: '/login', query: { redirect: to.fullPath } })
|
||||
}
|
||||
} catch (error) {
|
||||
next('/login')
|
||||
}
|
||||
})
|
||||
|
||||
export default router
|
||||
Reference in New Issue
Block a user