
next.js 13+ 中,当在动态路由下使用 `` 组件进行相对路径导航时,直接使用相对路径可能导致错误重定向。本文将深入探讨此问题,并提供基于 `usepathname` 钩子的解决方案,指导开发者如何正确地构建和拼接动态 url,确保 `` 组件在复杂路由结构中实现预期行为,从而优化用户导航体验。
在 Next.js 的文件系统路由中,开发者经常会遇到需要从一个动态路由页面导航到其子页面或同级页面的情况。例如,在一个路径结构为 /dashboard/[appid]/users/[userid] 的页面中,我们可能希望导航到 /dashboard/[appid]/users/[userid]/user_info。直观上,许多开发者会尝试在 <Link> 组件中使用相对路径,如 href="user_info"。
然而,在 Next.js 13+ 中,尤其是在动态路由的深层嵌套中,这种简单的相对路径引用往往不会按预期工作。它可能不会相对于当前 URL 进行拼接,而是被解析为根路径下的相对路径,即 /user_info,从而导致 404 错误或不正确的页面跳转。尽管浏览器在悬停时可能显示正确的完整 URL,但实际点击行为却与预期不符。这通常是因为 <Link> 组件在处理动态路由上下文中的相对路径时,其内部解析机制可能将其视为相对于应用程序根目录的路径。
为了在 Next.js 13+ 的动态路由中实现可靠的相对路径导航,我们需要明确地获取当前路径,并手动将其与目标路径段拼接起来。Next.js 13+ 提供了 usePathname 钩子(来自 next/navigation)来解决这个问题。
usePathname 是一个客户端钩子,它允许你在客户端组件中获取当前 URL 的路径部分(例如,对于 http://localhost:3000/dashboard/app_01/users/123/profile,usePathname 将返回 /dashboard/app_01/users/123/profile)。通过结合 usePathname,我们可以构建出完整的、正确的绝对路径,供 <Link> 组件使用。
假设我们有一个 SideBarButton 组件,它需要从当前用户页面导航到 user_info 子页面。以下是如何使用 usePathname 进行改造:
'use client'; // 声明为客户端组件,因为 usePathname 是客户端钩子
import Link from 'next/link';
import React from 'react';
import Image from 'next/image';
import { usePathname } from 'next/navigation'; // 导入 usePathname 钩子
type SidebarButtonProps = {
text: string,
to?: string, // 目标路径段,例如 "user_info"
id: string,
active?: string,
disabled?: string[],
icon?: string,
getButtonValue: (buttonid: string) => void
}
export const SideBarButton = ({ text, to: linkSegment, id, active, disabled, icon, getButtonValue }: SidebarButtonProps) => {
const currentPath = usePathname(); // 获取当前页面的路径
let buttonActive: boolean = id === active;
let buttonDisabled: boolean | undefined = disabled?.includes(id);
// 构建完整的 href 路径
// 确保 currentPath 不以斜杠结尾,除非它是根路径
const basePath = currentPath === '/' ? '' : currentPath;
const fullHref = linkSegment ? `${basePath}/${linkSegment}` : '';
const handleClick = (e: React.MouseEvent<HTMLElement>) => {
if (!buttonDisabled) {
getButtonValue(id);
}
}
return (
<Link
className="sidebar-button"
role='link'
href={!buttonDisabled ? fullHref : ''} // 使用构建的完整路径
replace={false}
onClick={handleClick}
>
<div className={`sidebar-button__container${buttonActive ? ' button_active' : ''}${buttonDisabled ? ' button_disabled' : ''}`} id={id}>
<div className='sidebar-button__svg'>
{icon && <Image src={icon} alt="icon" width={25} height={25} />}
</div>
<div className='sidebar-button__text'>
<span className='sidebar-button__span'>
{text}
</span>
</div>
</div>
</Link>
);
}在上述代码中,我们做了以下关键改动:
通过这种方式,无论当前页面的动态路由参数如何,fullHref 都会被正确地计算出来,确保 <Link> 组件导航到正确的 URL。
在 Next.js 13+ 的动态路由环境中,直接使用 <Link> 组件的相对路径 href 属性可能导致非预期的导航行为。为了确保在复杂路由结构中的准确导航,推荐使用 usePathname 钩子获取当前路径,并手动拼接目标路径段来构建完整的绝对 URL。这种方法虽然需要多一步操作,但能极大地提高导航的可靠性和可预测性,从而为用户提供更流畅的体验。
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号