Astro 入门指南(二):Content Collections
什么是 Content Collections
Content Collections 是 Astro 管理本地内容的核心功能。它提供了:
- 类型安全 — Zod schema 验证
- 自动类型推导 — 编辑器智能提示
- 统一 API — 一致的内容查询方式
定义集合
在 src/content/config.ts 中定义:
import { defineCollection, z } from "astro:content";
const blog = defineCollection({
type: "content",
schema: z.object({
title: z.string(),
date: z.date(),
}),
});
查询内容
import { getCollection } from "astro:content";
const posts = await getCollection("blog");
这就是 Astro 内容管理的基础。下一篇我们会聊聊组件和岛屿架构。