Skip to content

Commit b03ed49

Browse files
i18n(zh-cn): Update routing-reference.mdx (#12475)
Co-authored-by: Yan <[email protected]>
1 parent 04158d0 commit b03ed49

File tree

1 file changed

+30
-0
lines changed

1 file changed

+30
-0
lines changed

src/content/docs/zh-cn/reference/routing-reference.mdx

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -170,6 +170,36 @@ const { post } = Astro.props;
170170
<h1>{id}: {post.name}</h1>
171171
```
172172

173+
### `routePattern`
174+
175+
<p>
176+
177+
**类型:** `string` <br />
178+
<Since v="5.14.0" />
179+
</p>
180+
181+
[`getStaticPaths()`](#getstaticpaths) 选项中的一个属性,用于以字符串形式访问当前 [`routePattern`](/zh-cn/reference/api-reference/#routepattern)
182+
183+
该功能提供来自 [Astro 渲染上下文](/zh-cn/reference/api-reference/) 的数据,这些数据在 `getStaticPaths()` 作用域内通常无法获取,可用于计算每个页面路由的 `params``props`
184+
185+
与反映页面显式值(例如 `/fr/fichiers/article-1/` )的 `params` 不同, `routePattern` 始终反映文件路径中的原始动态段定义(例如 `/[...locale]/[files]/[slug]` )。
186+
187+
以下示例展示了如何通过将 `routePattern` 传递给自定义 `getLocalizedData()` 辅助函数来本地化路由段并返回静态路径数组。[params](/zh-cn/reference/routing-reference/#params) 对象将为每个路由段(`locale``files``slug`)设置明确值,随后这些值将用于生成路由,并可通过 `Astro.params` 在页面模板中使用。
188+
189+
```astro title="src/pages/[...locale]/[files]/[slug].astro" "routePattern" "getLocalizedData"
190+
---
191+
import { getLocalizedData } from "../../../utils/i18n";
192+
export async function getStaticPaths({ routePattern }) {
193+
const response = await fetch('...');
194+
const data = await response.json();
195+
console.log(routePattern); // [...locale]/[files]/[slug]
196+
// 结合 `routePattern` 调用你的自定义辅助函数以生成静态路径
197+
return data.flatMap((file) => getLocalizedData(file, routePattern));
198+
}
199+
const { locale, files, slug } = Astro.params;
200+
---
201+
```
202+
173203
### `paginate()`
174204

175205
<p>

0 commit comments

Comments
 (0)