Creation:2026-06-22Last update:2026-06-22
将此文档参考到您的 AI 助手ChatGPTClaudeDeepSeekGoogle AI modeGeminiPerplexityMistralGrok
使用您最喜欢的AI助手总结文档,并引用此页面和AI提供商
版本历史
- "添加 usePathname 实用程序"v10.0.02026/6/23
- "初始化历史记录"v8.2.02026/6/22
此页面的内容已使用 AI 翻译。
查看英文原文的最新版本Edit this doc
If you have an idea for improving this documentation, please feel free to contribute by submitting a pull request on GitHub.
GitHub link to the documentationCopy
Copy doc Markdown to clipboard
Solid 集成: usePathname Hook 文档
usePathname Hook 返回当前浏览器路径 (pathname),并去除了语言区域 (locale) 片段,类型为 Solid 的 Accessor<string>。它在构建包含本地化的导航栏时非常有用(例如,确定哪个导航项处于活动状态),无需手动删除 locale 前缀。
在 Solid 中导入 usePathname
typescript
复制代码
复制代码到剪贴板
import { usePathname } from "solid-intlayer";概览
usePathname 创建一个从 window.location.pathname 初始化的响应式信号 (reactive signal),通过 getPathWithoutLocale 剥离 locale 前缀,并在浏览器触发 popstate 事件(后退/前进导航)时更新信号。事件监听器通过 onCleanup 自动清理。
用法
src/components/NavItem.tsx
复制代码
复制代码到剪贴板
import type { Component } from "solid-js";import { usePathname } from "solid-intlayer";type NavItemProps = { href: string; label: string;};const NavItem: Component<NavItemProps> = ({ href, label }) => { const pathname = usePathname(); return ( <a href={href} aria-current={pathname() === href ? "page" : undefined}> {label} </a> );};export default NavItem;返回值
显示表格的所有内容
在弹窗中打开表格以清晰地查看所有数据
| 类型 | 描述 |
|---|---|
Accessor<string> | Solid Accessor(响应式 getter),返回不包含 locale 前缀的当前路径 (pathname)。 |
行为
- 剥离 Locale: 移除路径首部的 locale 片段(例如:
/zh/dashboard→/dashboard)。 - 响应式: 在触发
popstate事件(浏览器后退 / 前进导航)时自动更新。 - SSR 安全: 当
window不可用时返回""。 - 清理:
popstate监听器会通过 Solid 的onCleanup自动移除。
示例
src/components/Sidebar.tsx
复制代码
复制代码到剪贴板
import type { Component } from "solid-js";import { For } from "solid-js";import { usePathname } from "solid-intlayer";const links = [ { href: "/dashboard", label: "仪表盘" }, { href: "/settings", label: "设置" },];const Sidebar: Component = () => { const pathname = usePathname(); return ( <nav> <For each={links}> {({ href, label }) => ( <a href={href} style={{ "font-weight": pathname() === href ? "bold" : "normal" }} > {label} </a> )} </For> </nav> );};export default Sidebar;相关文档
useLocale— 当前语言区域 + 切换语言区域getPathWithoutLocale— 此 Hook 底层使用的工具函数