From ec3bb3d04f4620f6df5dc4999e9354d3c31c3cf0 Mon Sep 17 00:00:00 2001 From: xuewuerduo <53554701+xuewuerduo@users.noreply.github.com> Date: Mon, 15 Jul 2024 21:54:19 +0800 Subject: [PATCH] =?UTF-8?q?15.Vue=20Router=20=E8=BA=AB=E4=BB=BD=E9=AA=8C?= =?UTF-8?q?=E8=AF=81-=E9=A1=B5=E9=9D=A2=E6=8B=A6=E6=88=AA?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/router/index.js | 25 +++++++++++++++++++++++-- 1 file changed, 23 insertions(+), 2 deletions(-) diff --git a/src/router/index.js b/src/router/index.js index a4afb0f..2af7f10 100644 --- a/src/router/index.js +++ b/src/router/index.js @@ -1,4 +1,5 @@ import { createRouter, createWebHistory } from "vue-router" +import {useAdminStore} from "@/stores/admin/admin.js"; const routes= [ { path: "/login", // http://localhost:5173/login @@ -6,8 +7,8 @@ const routes= [ }, { path: "/admin", // http://localhost:5173/admin - component: () => import("@/views/admin/home.vue") - + component: () => import("@/views/admin/home.vue"), + meta: { requiresAuth: true }, //身份验证 } ] @@ -15,4 +16,24 @@ const router = createRouter({ history: createWebHistory(), routes }) + +router.beforeEach((to, from, next) => { + // console.log(to.meta.requiresAuth) + if ( to.meta.requiresAuth ){ + console.log("需要身份验证") + + //初始化 + const adminStore = useAdminStore() + if ( adminStore.data.token === "" ){ + console.log("未登录") + router.push("/login") + } + }else{ + console.log("不需要身份验证") + } + next() +}) + + + export default router