路由
Astro 使用基于文件的路由,它根据项目 src/pages 目录中的文件结构来生成你的构建链接。
页面之间的导航
Section titled “页面之间的导航”Astro 使用标准 HTML <a> 元素 在路由之间导航。 没有提供特定框架的 <Link> 组件。
<p>Read more <a href="/about/">about</a> Astro!</p>
<!-- 配置了 `base: "/docs"` --><p>Learn more in our <a href="/docs/reference/">reference</a> section!</p>src/pages 目录中的 .astro 页面组件以及 Markdown 和 MDX 文件(.md,.mdx)将自动作为网站页面。每个页面的路由对应于它在 src/pages/ 目录中的路径和文件名。
# 示例:静态路由src/pages/index.astro -> mysite.com/src/pages/about.astro -> mysite.com/aboutsrc/pages/about/index.astro -> mysite.com/aboutsrc/pages/about/me.astro -> mysite.com/about/mesrc/pages/posts/1.md -> mysite.com/posts/1Astro 项目没有单独的路由配置!当你在 src/pages 目录新增文件时,会自动生成一个新的路由。在静态生成中,你可以使用 build.format 配置项自定义文件输出格式。
Astro 页面文件可以在其文件名中指定动态路由参数以生成多个匹配页面。
例如,src/pages/authors/[author].astro 可以为你博客上的每一位作者生成一个简介页面,而 author 将作为一个你可以从页面内部访问的 参数 。
在 Astro 默认的静态输出模式中,这些页面是在构建时生成的,因此你必须预设对应文件的 author 列表。而在 SSR 模式下,将根据请求为任何匹配的路由生成一个页面。
静态 (SSG) 模式
Section titled “静态 (SSG) 模式”因为必须在构建时确定所有路由,所以动态路由必须导出一个 getStaticPaths() ,它返回一个具有 params 属性的对象数组。数组中的每一个对象都会生成相应的路由。
[dog].astro 在其文件名中定义一个 dog 参数,则 getStaticPaths() 返回的这些对象的 params 中必须包含 dog 。
然后页面可以使用 Astro.params 访问该参数。
---export function getStaticPaths() { return [ { params: { dog: "clifford" }}, { params: { dog: "rover" }}, { params: { dog: "spot" }}, ];}
const { dog } = Astro.params;---<div>Good dog, {dog}!</div>这将生成三个页面: /dogs/clifford、/dogs/rover 和 /dogs/spot ,每个页面显示相应的狗名。
文件名可以包含多个参数,这些参数必须都包含在 getStaticPaths() 的 params 对象中:
---export function getStaticPaths() { return [ { params: { lang: "en", version: "v1" }}, { params: { lang: "fr", version: "v2" }}, ];}
const { lang, version } = Astro.params;---...这将生成 /en-v1/info 和 /fr-v2/info 路由。
参数也可以包含在路径的单独部分中。例如, src/pages/[lang]/[version]/info.astro 文件与上面相同的 getStaticPaths() 将生成路由 /en/v1/info 和 /fr/v2/info。
解码 params
Section titled “解码 params”getStaticPaths() 函数返回的 params 不会被解码。当你需要解码参数值时,请使用 decodeURI()。
---export function getStaticPaths() { return [ { params: { slug: decodeURI("%5Bpage%5D") }}, // 解码成 "[page]" ]}---getStaticPaths() 的内容.
如果你的URL路由需要更加灵活,你可以在 .astro 文件名中使用一个 剩余参数([...path]),去匹配任何深度的文件路径。
---export function getStaticPaths() { return [ { params: { path: "one/two/three" }}, { params: { path: "four" }}, { params: { path: undefined }} ]}
const { path } = Astro.params;---这将生成 /sequences/one/two/three 、 /sequences/four 和 /sequences 。(将剩余参数设置为 undefined 允许它匹配顶级页面。)
剩余参数可以与 其他命名参数 一起使用。例如, GitHub 的文件查看器可以用以下动态路由表示:
/[org]/[repo]/tree/[branch]/[...file]在这个示例中,对 /withastro/astro/tree/main/docs/public/favicon.svg 的请求将拆分为以下命名参数:
{ org: "withastro", repo: "astro", branch: "main", file: "docs/public/favicon.svg"}示例:多级动态页面
Section titled “示例:多级动态页面”在下面的示例中, 剩余参数([...slug])和 getStaticPaths() 的 props 为不同深度的 slug 生成页面。
---export function getStaticPaths() { const pages = [ { slug: undefined, title: "Astro Store", text: "Welcome to the Astro store!", }, { slug: "products", title: "Astro products", text: "We have lots of products for you", }, { slug: "products/astro-handbook", title: "The ultimate Astro handbook", text: "If you want to learn Astro, you must read this book.", }, ];
return pages.map(({ slug, title, text }) => { return { params: { slug }, props: { title, text }, }; });}
const { title, text } = Astro.props;---<html> <head> <title>{title}</title> </head> <body> <h1>{title}</h1> <p>{text}</p> </body></html>