Skip to content

Astro 速查表

面向内容驱动网站的 Web 框架——默认零 JS 上线,速度极快。

01

快速上手

组件

在 --- 之间写 frontmatter,其后是 HTML 模板。

astro
---
const title = "Hello";
---

<h1>{title}</h1>

Props

通过 Astro.props 接收 props。

astro
---
interface Props { name: string; }
const { name } = Astro.props;
---

<h1>Hello {name}</h1>

页面

基于 src/pages/ 目录的文件路由。

astro
---
// src/pages/index.astro
---
<html><body>Hello</body></html>
02

核心特性

岛屿架构(Islands)

使用 client: 指令控制水合时机:load、idle、visible、only。

astro
---
import Counter from "../components/Counter.tsx";
---

<Counter client:load />

动态路由

使用 getStaticPaths 生成动态 SSG 路由。

astro
---
export async function getStaticPaths() {
  return posts.map(p => ({ params: { slug: p.slug }, props: p }));
}
const post = Astro.props;
---

内容集合

类型安全的内容集合。

astro
---
import { getCollection } from "astro:content";
const posts = await getCollection("blog");
---

这篇内容对您有帮助吗?