2025-07-22 14:39:45 +08:00
|
|
|
import { createRouter, createWebHistory } from 'vue-router'
|
|
|
|
import type { RouteRecordRaw } from 'vue-router'
|
|
|
|
|
|
|
|
// 导入页面组件
|
|
|
|
import Home from '@/views/Home.vue'
|
|
|
|
import Courses from '@/views/Courses.vue'
|
|
|
|
import CourseDetail from '@/views/CourseDetail.vue'
|
|
|
|
import Learning from '@/views/Learning.vue'
|
|
|
|
import Profile from '@/views/Profile.vue'
|
|
|
|
import Login from '@/views/Login.vue'
|
|
|
|
import Register from '@/views/Register.vue'
|
2025-07-22 20:59:46 +08:00
|
|
|
import LearningPaths from '@/views/LearningPaths.vue'
|
2025-07-22 14:39:45 +08:00
|
|
|
|
|
|
|
const routes: RouteRecordRaw[] = [
|
|
|
|
{
|
|
|
|
path: '/',
|
|
|
|
name: 'Home',
|
|
|
|
component: Home,
|
|
|
|
meta: {
|
|
|
|
title: '首页'
|
|
|
|
}
|
|
|
|
},
|
|
|
|
{
|
|
|
|
path: '/courses',
|
|
|
|
name: 'Courses',
|
|
|
|
component: Courses,
|
|
|
|
meta: {
|
|
|
|
title: '课程列表'
|
|
|
|
}
|
|
|
|
},
|
|
|
|
{
|
|
|
|
path: '/course/:id',
|
|
|
|
name: 'CourseDetail',
|
|
|
|
component: CourseDetail,
|
|
|
|
meta: {
|
|
|
|
title: '课程详情'
|
|
|
|
}
|
|
|
|
},
|
|
|
|
{
|
|
|
|
path: '/learning/:id',
|
|
|
|
name: 'Learning',
|
|
|
|
component: Learning,
|
|
|
|
meta: {
|
|
|
|
title: '学习中心',
|
|
|
|
requiresAuth: true
|
|
|
|
}
|
|
|
|
},
|
|
|
|
{
|
|
|
|
path: '/profile',
|
|
|
|
name: 'Profile',
|
|
|
|
component: Profile,
|
|
|
|
meta: {
|
|
|
|
title: '个人中心',
|
|
|
|
requiresAuth: true
|
|
|
|
}
|
|
|
|
},
|
|
|
|
{
|
|
|
|
path: '/login',
|
|
|
|
name: 'Login',
|
|
|
|
component: Login,
|
|
|
|
meta: {
|
|
|
|
title: '登录'
|
|
|
|
}
|
|
|
|
},
|
|
|
|
{
|
|
|
|
path: '/register',
|
|
|
|
name: 'Register',
|
|
|
|
component: Register,
|
|
|
|
meta: {
|
|
|
|
title: '注册'
|
|
|
|
}
|
|
|
|
},
|
2025-07-22 20:59:46 +08:00
|
|
|
{
|
|
|
|
path: '/learning-paths',
|
|
|
|
name: 'LearningPaths',
|
|
|
|
component: LearningPaths,
|
|
|
|
meta: {
|
|
|
|
title: '学习路径'
|
|
|
|
}
|
|
|
|
},
|
2025-07-22 14:39:45 +08:00
|
|
|
{
|
|
|
|
path: '/:pathMatch(.*)*',
|
|
|
|
name: 'NotFound',
|
|
|
|
component: () => import('@/views/NotFound.vue'),
|
|
|
|
meta: {
|
|
|
|
title: '页面未找到'
|
|
|
|
}
|
|
|
|
}
|
|
|
|
]
|
|
|
|
|
|
|
|
const router = createRouter({
|
|
|
|
history: createWebHistory(),
|
|
|
|
routes,
|
2025-07-22 16:23:15 +08:00
|
|
|
scrollBehavior(_to, _from, savedPosition) {
|
2025-07-22 14:39:45 +08:00
|
|
|
if (savedPosition) {
|
|
|
|
return savedPosition
|
|
|
|
} else {
|
|
|
|
return { top: 0 }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
// 路由守卫
|
2025-07-22 16:23:15 +08:00
|
|
|
router.beforeEach((to, _from, next) => {
|
2025-07-22 14:39:45 +08:00
|
|
|
// 设置页面标题
|
|
|
|
if (to.meta.title) {
|
|
|
|
document.title = `${to.meta.title} - 在线学习平台`
|
|
|
|
}
|
|
|
|
|
|
|
|
// 检查是否需要登录
|
|
|
|
if (to.meta.requiresAuth) {
|
|
|
|
// 这里可以检查用户登录状态
|
|
|
|
// 暂时跳过认证检查
|
|
|
|
next()
|
|
|
|
} else {
|
|
|
|
next()
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
export default router
|