Step F1:前端工程骨架 & 登录鉴权

This commit is contained in:
Cheng Zhou
2025-12-16 20:21:30 +08:00
parent 5eab324c50
commit 78d13d077c
25 changed files with 523 additions and 1 deletions
+48
View File
@@ -0,0 +1,48 @@
import { createRouter, createWebHistory, RouteRecordRaw } from "vue-router";
import { useAuthStore } from "../store/auth";
import Layout from "../components/Layout.vue";
import Home from "../views/Home.vue";
import Login from "../views/Login.vue";
const routes: RouteRecordRaw[] = [
{
path: "/login",
name: "Login",
component: Login,
meta: { public: true, title: "登录" },
},
{
path: "/",
component: Layout,
redirect: "/home",
children: [
{
path: "home",
name: "Home",
component: Home,
meta: { title: "首页" },
},
],
},
];
const router = createRouter({
history: createWebHistory(),
routes,
});
router.beforeEach((to, _from, next) => {
const auth = useAuthStore();
const token = auth.token;
if (!to.meta.public && !token) {
next({ path: "/login" });
return;
}
if (to.path === "/login" && token) {
next({ path: "/" });
return;
}
next();
});
export default router;