组件
<NuxtLinkLocale>
用于结合 <NuxtLink> 使用 localePath 的简写组件
此组件基于 <NuxtLink> 构建,但通过内部使用 localePath() 改变了默认行为,使链接到本地化路由更加方便。
Props
此组件支持所有 <NuxtLink> 文档中描述的 props,并额外支持下面描述的 props。
| 属性 | 说明 |
|---|---|
locale | 可选属性,用于强制使用传入的语言环境进行本地化,默认为当前语言环境。与 localePath() 的 locale 参数相同。 |
示例
基本用法
to prop 接受路由名称,或使用 name 属性的路由对象:
<template>
<NuxtLinkLocale to="index">{{ $t('home') }}</NuxtLinkLocale>
<NuxtLinkLocale :to="{ name: 'index' }">{{ $t('home') }}</NuxtLinkLocale>
</template>
<!-- 等同于 -->
<script setup>
const localePath = useLocalePath()
</script>
<template>
<NuxtLink :to="localePath('index')">{{ $t('home') }}</NuxtLink>
<NuxtLink :to="localePath({ name: 'index' })">{{ $t('home') }}</NuxtLink>
</template>
强制指定语言环境
<template>
<NuxtLinkLocale to="index" locale="nl">{{ $t('home') }}</NuxtLinkLocale>
</template>
<!-- 等同于 -->
<script setup>
const localePath = useLocalePath()
</script>
<template>
<NuxtLink :to="localePath('index', 'nl')">{{ $t('home') }}</NuxtLink>
</template>
路径字符串
传递路径字符串在运行时有效,并为保持向后兼容而保留:
<template>
<NuxtLinkLocale to="/">{{ $t('home') }}</NuxtLinkLocale>
</template>
我们建议使用命名路由(如上面的示例所示),而不是路径字符串。路径取决于配置的
strategy 和自定义路径,这意味着今天能够正确解析的路径,可能会在路由配置发生变化时无提示地停止匹配。因此,当启用 typedPages 时,to prop 在类型层面上只接受命名路由。
:::