useSelectedLayoutSegments
useSelectedLayoutSegments 是一个**客户端组件**钩子,它允许您读取其被调用的布局**下方**的活动路由段。
它对于在父布局中创建需要了解活动子段(如面包屑)的 UI 非常有用。
app/example-client-component.tsx
'use client'
 
import { useSelectedLayoutSegments } from 'next/navigation'
 
export default function ExampleClientComponent() {
  const segments = useSelectedLayoutSegments()
 
  return (
    <ul>
      {segments.map((segment, index) => (
        <li key={index}>{segment}</li>
      ))}
    </ul>
  )
}须知:
参数
const segments = useSelectedLayoutSegments(parallelRoutesKey?: string)useSelectedLayoutSegments 可选地接受一个parallelRoutesKey,它允许您读取该插槽中的活动路由段。
返回
useSelectedLayoutSegments 返回一个字符串数组,其中包含从调用该钩子的布局下一层开始的活动段。如果没有,则返回一个空数组。
例如,给定以下布局和 URL,返回的段将是:
| 布局 | 访问的 URL | 返回的段 | 
|---|---|---|
| app/layout.js | / | [] | 
| app/layout.js | /dashboard | ['dashboard'] | 
| app/layout.js | /dashboard/settings | ['dashboard', 'settings'] | 
| app/dashboard/layout.js | /dashboard | [] | 
| app/dashboard/layout.js | /dashboard/settings | ['settings'] | 
版本历史
| 版本 | 更改 | 
|---|---|
| v13.0.0 | useSelectedLayoutSegments已引入。 | 
这有帮助吗?