初始化仓库
This commit is contained in:
53
src/router/index.ts
Normal file
53
src/router/index.ts
Normal file
@@ -0,0 +1,53 @@
|
||||
import { createRouter, createWebHistory } from 'vue-router';
|
||||
|
||||
const whiteList = ["/login"];
|
||||
|
||||
const routes = [
|
||||
{
|
||||
path: '/',
|
||||
name: 'Index',
|
||||
component: () => import('@/views/index.vue')
|
||||
},
|
||||
{
|
||||
path: '/login',
|
||||
name: 'Login',
|
||||
component: () => import('@/views/login.vue')
|
||||
},
|
||||
{
|
||||
path: '/pwoManage',
|
||||
name: 'PwoManage',
|
||||
component: () => import('@/views/pwoManage/index.vue')
|
||||
}
|
||||
];
|
||||
|
||||
const router = createRouter({
|
||||
history: createWebHistory(),
|
||||
routes
|
||||
});
|
||||
|
||||
router.beforeEach(async (to, from, next) => {
|
||||
const token = localStorage.getItem("token");
|
||||
|
||||
// 白名单放行
|
||||
if (whiteList.includes(to.path)) {
|
||||
return next();
|
||||
}
|
||||
|
||||
// 已登录,访问 login → 跳首页
|
||||
if (token && to.path === "/login") {
|
||||
return next("/");
|
||||
}
|
||||
|
||||
// 未登录,访问受保护路由
|
||||
if (!token) {
|
||||
return next({
|
||||
path: "/login",
|
||||
query: { redirect: to.fullPath },
|
||||
});
|
||||
}
|
||||
|
||||
// 已登录,正常访问
|
||||
next();
|
||||
});
|
||||
|
||||
export default router;
|
||||
Reference in New Issue
Block a user