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
Vue 集成:usePathname 文档
usePathname 函数返回当前浏览器的路径名(pathname),并以 Vue ComputedRef<string> 的形式提供移除了 locale 片段后的路径名。这对于构建可感知 locale 的导航非常有用——例如,确定哪个导航项处于激活状态——而无需手动移除 locale 前缀。
在 Vue 中导入 usePathname
typescript
复制代码
复制代码到剪贴板
import { usePathname } from "vue-intlayer";概览
usePathname 创建一个 Vue 计算引用 (computed ref),它读取 window.location.pathname,通过 getPathWithoutLocale 移除 locale 前缀,并且每当浏览器触发 popstate 事件(后退/前进导航)时自动更新其值。该值可以直接在你的 Vue 模板或 setup 函数中使用。
用法
src/components/NavItem.vue
复制代码
复制代码到剪贴板
<script setup lang="ts">import { usePathname } from "vue-intlayer";const props = defineProps<{ href: string; label: string;}>();const pathname = usePathname();</script><template> <a :href="href" :aria-current="pathname === href ? 'page' : undefined"> {{ label }} </a></template>返回值
显示表格的所有内容
在弹窗中打开表格以清晰地查看所有数据
| 类型 | 描述 |
|---|---|
ComputedRef<string> | Vue Computed Ref,包含不带 locale 前缀的当前路径名(pathname)。 |
行为
- Locale 剥离 (Locale stripping):移除路径开头的 locale 片段(例如:
/zh/dashboard→/dashboard)。 - 响应式 (Reactive):在每次
popstate事件(浏览器后退 / 前进导航)时更新值。 - SSR 安全 (SSR-safe):当
window不可用时返回""。 - 清理 (Cleanup):
popstate监听器在初始化时全局添加,由于 Vue 对生命周期的管理,通常不需要在每个组件中手动进行清理。
示例
src/components/Sidebar.vue
复制代码
复制代码到剪贴板
<script setup lang="ts">import { usePathname } from "vue-intlayer";const links = [ { href: "/dashboard", label: "仪表盘" }, { href: "/settings", label: "设置" },];const pathname = usePathname();</script><template> <nav> <a v-for="link in links" :key="link.href" :href="link.href" :style="{ fontWeight: pathname === link.href ? 'bold' : 'normal' }" > {{ link.label }} </a> </nav></template>相关内容
useLocale— 当前 locale 及其切换器getPathWithoutLocale— 此 hook 所使用的底层实用函数