feat(router): 添加页面标题和滚动重置功能
- 为路由添加 meta.title 配置,在导航守卫中动态设置文档标题 - 添加 afterEach 钩子,在路由切换时重置布局内容和窗口滚动位置 - 标准化路由配置的逗号格式,增强代码一致性 - 优化主题切换 composable 的代码格式和注释翻译 - 改进访问统计页面的排序逻辑,使用 computed 替代手动重新加载 - 添加全局滚动条样式,提升视觉一致性
This commit is contained in:
@@ -11,7 +11,7 @@ const routes: RouteRecordRaw[] = [
|
||||
{
|
||||
path: '/login',
|
||||
name: 'login',
|
||||
component: LoginView
|
||||
component: LoginView,
|
||||
},
|
||||
{
|
||||
path: '/',
|
||||
@@ -19,43 +19,64 @@ const routes: RouteRecordRaw[] = [
|
||||
children: [
|
||||
{
|
||||
path: '',
|
||||
redirect: '/comments'
|
||||
redirect: '/comments',
|
||||
},
|
||||
{
|
||||
path: 'comments',
|
||||
name: 'comments',
|
||||
component: CommentsView
|
||||
component: CommentsView,
|
||||
meta: {
|
||||
title: '评论管理',
|
||||
},
|
||||
},
|
||||
{
|
||||
path: 'stats',
|
||||
name: 'stats',
|
||||
component: StatsView
|
||||
component: StatsView,
|
||||
meta: {
|
||||
title: '数据看板',
|
||||
},
|
||||
},
|
||||
{
|
||||
path: 'analytics',
|
||||
name: 'analytics',
|
||||
component: AnalyticsVisitView
|
||||
component: AnalyticsVisitView,
|
||||
meta: {
|
||||
title: '访问统计',
|
||||
},
|
||||
},
|
||||
{
|
||||
path: 'settings',
|
||||
name: 'settings',
|
||||
component: SettingsView
|
||||
component: SettingsView,
|
||||
meta: {
|
||||
title: '网站设置',
|
||||
},
|
||||
},
|
||||
{
|
||||
path: 'data',
|
||||
name: 'data',
|
||||
component: DataView
|
||||
}
|
||||
]
|
||||
}
|
||||
component: DataView,
|
||||
meta: {
|
||||
title: '数据迁移',
|
||||
},
|
||||
},
|
||||
],
|
||||
},
|
||||
];
|
||||
|
||||
export const router = createRouter({
|
||||
history: createWebHistory(),
|
||||
routes
|
||||
routes,
|
||||
});
|
||||
|
||||
router.beforeEach((to, from, next) => {
|
||||
const defaultTitle = 'CWD 评论系统';
|
||||
if (to.meta && to.meta.title) {
|
||||
document.title = (to.meta.title + ' - ' + defaultTitle) as string;
|
||||
} else {
|
||||
document.title = defaultTitle as string;
|
||||
}
|
||||
if (to.name === 'login') {
|
||||
next();
|
||||
return;
|
||||
@@ -67,3 +88,13 @@ router.beforeEach((to, from, next) => {
|
||||
}
|
||||
next();
|
||||
});
|
||||
|
||||
router.afterEach((to, from) => {
|
||||
if (to.name !== from.name) {
|
||||
const layoutContent = document.querySelector('.layout-content');
|
||||
if (layoutContent instanceof HTMLElement) {
|
||||
layoutContent.scrollTop = 0;
|
||||
}
|
||||
window.scrollTo(0, 0);
|
||||
}
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user