router.beforeEach((to, from, next) => { const currentUser = // 获取当前用户 const role = currentUser.role

const token = localStorage.getItem('token');

if (to.meta.requiresAuth && !token) { alert("请先登录!"); // 需要鉴权的路由且未登录,跳转到登录页面 next('/login'); } else if (!to.meta.requiresAuth && token) { // 不需要鉴权的路由且已登录,跳转到首页 alert("欢迎您管理员!"); next('/home'); } else if (to.meta.role && to.meta.role !== role) { // 用户角色不符合要求,重定向到其他页面或提示权限不足 next('/home'); } else { // 用户角色符合要求,继续访问路由 next("/login") } }

routerbeforeEachto from next = const currentUser = 获取当前用户 const role = currentUserrole if tometarole && tometarole !== role 用户角色不符合要求重定向到其他页面或提示权限不足 nextteacherHome else 用户角色符合要求继续

原文地址: https://www.cveoy.top/t/topic/h4mF 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录