feat: 编写路由

frontend/dev
Spark 2 months ago
parent 858e06b1e1
commit 9343419bfb

@ -0,0 +1,66 @@
// router/index.js
import { createRouter, createWebHistory } from 'vue-router'
import StudentManageView from "@/views/Manage.vue";
import LayoutView from '@/components/Layout.vue';
import HomeView from '@/views/Home.vue'
import LoginView from '@/views/Login.vue'
import RollCallView from '@/views/RollCall.vue'
import { useTokenStore } from "@/store/token.js";
const routes = [
{
path: '/login',
name: 'login',
component: LoginView
},
{
path: '/',
component: LayoutView,
children: [
{
path: '',
redirect: '/home'
},
{
path: '/home',
name: 'home',
component: HomeView
},
{
path: '/rollcall',
name: 'rollcall',
component: RollCallView
},
{
path: '/student',
name: 'student',
component: StudentManageView
}
]
}
]
const router = createRouter({
history: createWebHistory(),
routes
});
export function setupRouterGuard(router) {
router.beforeEach((to, from, next) => {
const tokenStore = useTokenStore()
const isAuthenticated = tokenStore.token !== ''
if (to.name !== 'login' && !isAuthenticated) {
next({ name: 'login' })
} else if (to.name === 'login' && isAuthenticated) {
next({ name: 'home' })
} else {
next()
}
})
}
export default router
Loading…
Cancel
Save