1 找到这样一个地方,前置路由写在这里
1const router = createRouter({ history: createWebHistory(import.meta.env.BASE_URL), routes: routes }); export default router;
2 完整代码
const router = createRouter({ history: createWebHistory(import.meta.env.BASE_URL), routes: routes }); // 路由前置守卫(防止用户手动输入路径跳过登录直接进入页面) router.beforeEach((to, from, next) => { const token = state.token; if (token) { next(); } else { if (to.path == "/login") { next(); } else { next({ path: "/login", }); } } }); export default router;