usePathname
usePathname 是一个客户端组件的hook,可用于读取当前 URL 的路径。
'use client'
import { usePathname } from 'next/navigation'
export default function ExampleClientComponent() {
const pathname = usePathname()
return <p>Current pathname: {pathname}</p>
}
example
Url | Value |
---|---|
/ | / |
/dashboard | /dashboard |
/dashboard?v=2 | /dashboard |
/blog/hello-world | /blog/hello-world |