初版代码提交
This commit is contained in:
@@ -0,0 +1,119 @@
|
||||
import { createRouter, createWebHistory } from 'vue-router'
|
||||
|
||||
const routes = [
|
||||
{
|
||||
path: '/login',
|
||||
name: 'Login',
|
||||
component: () => import('@/views/Login.vue'),
|
||||
meta: { title: '登录' },
|
||||
},
|
||||
{
|
||||
path: '/register',
|
||||
name: 'Register',
|
||||
component: () => import('@/views/Register.vue'),
|
||||
meta: { title: '注册' },
|
||||
},
|
||||
{
|
||||
path: '/',
|
||||
component: () => import('@/layouts/BlogLayout.vue'),
|
||||
children: [
|
||||
{
|
||||
path: '',
|
||||
name: 'Home',
|
||||
component: () => import('@/views/Home.vue'),
|
||||
meta: { title: '首页' },
|
||||
},
|
||||
{
|
||||
path: 'article/:id',
|
||||
name: 'ArticleDetail',
|
||||
component: () => import('@/views/ArticleDetail.vue'),
|
||||
meta: { title: '文章详情' },
|
||||
},
|
||||
],
|
||||
},
|
||||
{
|
||||
path: '/app',
|
||||
component: () => import('@/layouts/AppLayout.vue'),
|
||||
meta: { requiresAuth: true },
|
||||
children: [
|
||||
{
|
||||
path: 'dashboard',
|
||||
name: 'Dashboard',
|
||||
component: () => import('@/views/app/Dashboard.vue'),
|
||||
meta: { title: '控制台', requiresAuth: true },
|
||||
},
|
||||
{
|
||||
path: 'articles',
|
||||
name: 'MyArticles',
|
||||
component: () => import('@/views/app/MyArticles.vue'),
|
||||
meta: { title: '我的文章', requiresAuth: true },
|
||||
},
|
||||
{
|
||||
path: 'editor',
|
||||
name: 'NewArticle',
|
||||
component: () => import('@/views/app/ArticleEditor.vue'),
|
||||
meta: { title: '写文章', requiresAuth: true },
|
||||
},
|
||||
{
|
||||
path: 'editor/:id',
|
||||
name: 'EditArticle',
|
||||
component: () => import('@/views/app/ArticleEditor.vue'),
|
||||
meta: { title: '编辑文章', requiresAuth: true },
|
||||
},
|
||||
],
|
||||
},
|
||||
{
|
||||
path: '/admin',
|
||||
component: () => import('@/layouts/AdminLayout.vue'),
|
||||
meta: { requiresAuth: true, requiresAdmin: true },
|
||||
children: [
|
||||
{
|
||||
path: '',
|
||||
name: 'AdminDashboard',
|
||||
component: () => import('@/views/admin/Dashboard.vue'),
|
||||
meta: { title: '管理后台', requiresAuth: true, requiresAdmin: true },
|
||||
},
|
||||
{
|
||||
path: 'users',
|
||||
name: 'AdminUsers',
|
||||
component: () => import('@/views/admin/Users.vue'),
|
||||
meta: { title: '用户管理', requiresAuth: true, requiresAdmin: true },
|
||||
},
|
||||
{
|
||||
path: 'articles',
|
||||
name: 'AdminArticles',
|
||||
component: () => import('@/views/admin/Articles.vue'),
|
||||
meta: { title: '文章管理', requiresAuth: true, requiresAdmin: true },
|
||||
},
|
||||
{
|
||||
path: 'comments',
|
||||
name: 'AdminComments',
|
||||
component: () => import('@/views/admin/Comments.vue'),
|
||||
meta: { title: '评论管理', requiresAuth: true, requiresAdmin: true },
|
||||
},
|
||||
{
|
||||
path: 'categories',
|
||||
name: 'AdminCategories',
|
||||
component: () => import('@/views/admin/Categories.vue'),
|
||||
meta: { title: '分类管理', requiresAuth: true, requiresAdmin: true },
|
||||
},
|
||||
],
|
||||
},
|
||||
]
|
||||
|
||||
const router = createRouter({
|
||||
history: createWebHistory(),
|
||||
routes,
|
||||
})
|
||||
|
||||
router.beforeEach((to, from, next) => {
|
||||
document.title = to.meta.title ? `${to.meta.title} - 博客系统` : '博客系统'
|
||||
const token = localStorage.getItem('access_token')
|
||||
if (to.meta.requiresAuth && !token) {
|
||||
next({ name: 'Login', query: { redirect: to.fullPath } })
|
||||
} else {
|
||||
next()
|
||||
}
|
||||
})
|
||||
|
||||
export default router
|
||||
Reference in New Issue
Block a user