From 8e63ecbd066d7d5ffdcf42ed0264e2f4b513230a Mon Sep 17 00:00:00 2001 From: tone <3341154833@qq.com> Date: Tue, 13 May 2025 11:35:17 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96header?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- tone-page-web/components/Header.tsx | 22 ++++++++++++++-------- 1 file changed, 14 insertions(+), 8 deletions(-) diff --git a/tone-page-web/components/Header.tsx b/tone-page-web/components/Header.tsx index 2fb4e24..c3a293a 100644 --- a/tone-page-web/components/Header.tsx +++ b/tone-page-web/components/Header.tsx @@ -2,7 +2,7 @@ import { cn } from "@/lib/utils"; import Link from "next/link"; -import { usePathname } from "next/navigation"; +import { usePathname, useRouter } from "next/navigation"; import { useCallback, useState } from "react"; import { Drawer, @@ -17,6 +17,7 @@ import { X } from "lucide-react"; export default function Header() { + const router = useRouter(); const pathname = usePathname(); const [showMenu, setShowMenu] = useState(false); @@ -27,12 +28,16 @@ export default function Header() { { name: '控制台', path: '/console' }, ]; - const getHref = useCallback((path: string) => { + const handleClick = (e: React.MouseEvent, path: string) => { + e.preventDefault(); if (path === '/console') { - return localStorage.getItem('token') ? '/console' : '/console/login'; + const token = typeof window !== 'undefined' ? localStorage.getItem('token') : null; + router.push(token ? '/console' : '/console/login'); + } else { + router.push(path); } - return path; - }, []); + setShowMenu(false); + } return (
@@ -68,8 +73,8 @@ export default function Header() { {menuItems.slice(1).map((item) => ( setShowMenu(false)} + href={item.path} + onClick={e => handleClick(e, item.path)} >