File tree Expand file tree Collapse file tree 1 file changed +30
-0
lines changed
src/content/docs/zh-cn/reference Expand file tree Collapse file tree 1 file changed +30
-0
lines changed Original file line number Diff line number Diff line change @@ -170,6 +170,36 @@ const { post } = Astro.props;
170
170
<h1>{id}: {post.name}</h1>
171
171
```
172
172
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
+
173
203
### ` paginate() `
174
204
175
205
<p >
You can’t perform that action at this time.
0 commit comments