64 lines
1.5 KiB
TypeScript
64 lines
1.5 KiB
TypeScript
import { createRouter, createWebHistory } from 'vue-router'
|
|
|
|
const router = createRouter({
|
|
history: createWebHistory(),
|
|
routes: [
|
|
{
|
|
path: '/login',
|
|
component: () => import('../views/Auth/Login.vue')
|
|
},
|
|
{
|
|
path: '/register',
|
|
component: () => import('../views/Auth/Register.vue')
|
|
},
|
|
{
|
|
path: '/',
|
|
component: () => import('../views/Layout/index.vue'),
|
|
children: [
|
|
{
|
|
path: 'dashboard',
|
|
component: () => import('../views/Dashboard/index.vue'),
|
|
meta: { requiresAuth: true }
|
|
},
|
|
{
|
|
path: 'user',
|
|
component: () => import('../views/User/index.vue'),
|
|
meta: { requiresAuth: true }
|
|
},
|
|
{
|
|
path: 'account',
|
|
component: () => import('../views/Account/index.vue'),
|
|
meta: { requiresAuth: true }
|
|
},
|
|
{
|
|
path: 'model',
|
|
component: () => import('../views/Model/index.vue'),
|
|
meta: { requiresAuth: true }
|
|
}
|
|
]
|
|
}
|
|
]
|
|
})
|
|
|
|
// 路由守卫
|
|
router.beforeEach((to, from, next) => {
|
|
console.log('路由跳转:', from.path, '->', to.path)
|
|
const token = localStorage.getItem('access_token')
|
|
console.log('当前token:', token)
|
|
|
|
if (to.meta.requiresAuth) {
|
|
if (!token) {
|
|
console.log('未授权访问,重定向到登录页')
|
|
next('/login')
|
|
} else {
|
|
console.log('已授权,允许访问')
|
|
next()
|
|
}
|
|
} else {
|
|
console.log('无需授权,允许访问')
|
|
next()
|
|
}
|
|
})
|
|
|
|
export default router
|