Commit 0912196060c100c605e8412fed78eebdaf25198e
1 parent
0ec38cbe
router跳转
Showing
2 changed files
with
41 additions
and
29 deletions
src/App.vue
| 1 | <script setup> | 1 | <script setup> |
| 2 | import { RouterLink, RouterView } from 'vue-router' | 2 | import { RouterLink, RouterView } from 'vue-router' |
| 3 | import {onMounted} from "vue"; | 3 | import {onMounted} from "vue"; |
| 4 | - | 4 | +import { useRouter } from 'vue-router' |
| 5 | +const router = useRouter() | ||
| 5 | onMounted(() => { | 6 | onMounted(() => { |
| 7 | + const isAuthenticated = sessionStorage.getItem('isAuthenticated') | ||
| 8 | + // 需要认证但未登录 → 跳转登录页 | ||
| 9 | + if (!isAuthenticated) { | ||
| 10 | + router.push('/login') | ||
| 11 | + }else{ | ||
| 12 | + // 已登录访问登录页 → 跳转首页 | ||
| 13 | + router.push('/') | ||
| 14 | + } | ||
| 6 | window.addEventListener('beforeunload', () => { | 15 | window.addEventListener('beforeunload', () => { |
| 7 | sessionStorage.removeItem('isAuthenticated') | 16 | sessionStorage.removeItem('isAuthenticated') |
| 8 | }) | 17 | }) |
src/router/index.js
| @@ -6,48 +6,51 @@ const router = createRouter({ | @@ -6,48 +6,51 @@ const router = createRouter({ | ||
| 6 | history: createWebHistory(import.meta.env.BASE_URL), | 6 | history: createWebHistory(import.meta.env.BASE_URL), |
| 7 | routes: [ | 7 | routes: [ |
| 8 | { | 8 | { |
| 9 | - path: '/', | ||
| 10 | - name: 'home', | 9 | + path: "/", |
| 10 | + redirect: "/home", | ||
| 11 | + }, | ||
| 12 | + { | ||
| 13 | + path: "/home", | ||
| 14 | + name: "home", | ||
| 11 | component: HomeView, | 15 | component: HomeView, |
| 12 | meta: { | 16 | meta: { |
| 13 | - title: 'SN管理', | ||
| 14 | - requiresAuth: true // 添加这行,标记该路由需要认证 | ||
| 15 | - } | ||
| 16 | - | 17 | + title: "SN管理", |
| 18 | + requiresAuth: true, // 添加这行,标记该路由需要认证 | ||
| 19 | + }, | ||
| 17 | }, | 20 | }, |
| 18 | { | 21 | { |
| 19 | - path: '/login', | ||
| 20 | - name: 'Login', | 22 | + path: "/login", |
| 23 | + name: "Login", | ||
| 21 | component: Login, | 24 | component: Login, |
| 22 | meta: { | 25 | meta: { |
| 23 | - requiresAuth: false // 明确表示登录页不需要认证 | ||
| 24 | - } | ||
| 25 | - } | ||
| 26 | - ] | ||
| 27 | -}) | 26 | + requiresAuth: false, // 明确表示登录页不需要认证 |
| 27 | + }, | ||
| 28 | + }, | ||
| 29 | + ], | ||
| 30 | +}); | ||
| 28 | 31 | ||
| 29 | // 路由守卫 | 32 | // 路由守卫 |
| 30 | -router.beforeEach((to, from, next) => { | ||
| 31 | - const isAuthenticated = sessionStorage.getItem('isAuthenticated') | 33 | +// router.beforeEach((to, from, next) => { |
| 34 | +// const isAuthenticated = sessionStorage.getItem('isAuthenticated') | ||
| 32 | 35 | ||
| 33 | - if (to.path === '/login' && !isAuthenticated) { | ||
| 34 | - return next('/login') | ||
| 35 | - } | 36 | +// // if (to.path === '/login' && !isAuthenticated) { |
| 37 | +// // return next('/login') | ||
| 38 | +// // } | ||
| 36 | 39 | ||
| 37 | - // 需要认证但未登录 → 跳转登录页 | ||
| 38 | - if (to.meta.requiresAuth && !isAuthenticated) { | ||
| 39 | - return next('/login') | ||
| 40 | - } | 40 | +// // 需要认证但未登录 → 跳转登录页 |
| 41 | +// if (to.meta.requiresAuth && !isAuthenticated) { | ||
| 42 | +// return next('/login') | ||
| 43 | +// } | ||
| 41 | 44 | ||
| 42 | - // 已登录但访问登录页 → 跳转首页 | ||
| 43 | - if (to.path === '/login' && isAuthenticated) { | ||
| 44 | - return next('/') | ||
| 45 | - } | 45 | +// // 已登录但访问登录页 → 跳转首页 |
| 46 | +// if (to.path === '/login' && isAuthenticated) { | ||
| 47 | +// return next('/') | ||
| 48 | +// } | ||
| 46 | 49 | ||
| 47 | 50 | ||
| 48 | 51 | ||
| 49 | - next() | ||
| 50 | -}) | 52 | +// next() |
| 53 | +// }) | ||
| 51 | 54 | ||
| 52 | 55 | ||
| 53 | export default router | 56 | export default router |