import { createRouter, createWebHistory } from 'vue-router'; import BattleRanking from '../views/BattleRanking.vue'; import AdminPanel from '../views/AdminPanel.vue'; const routes = [ { path: '/', name: 'BattleRanking', component: BattleRanking, meta: { title: '百日大战排行榜' } }, { path: '/admin', name: 'AdminPanel', component: AdminPanel, meta: { title: '管理员面板' } }, { // 捕获所有未匹配的路由,重定向到首页 path: '/:pathMatch(.*)*', redirect: '/' } ]; const router = createRouter({ history: createWebHistory(), routes }); // 全局前置守卫,设置页面标题 router.beforeEach((to, from, next) => { // 设置文档标题 if (to.meta.title) { document.title = to.meta.title; } next(); }); export default router;