astro
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
ChineseAstro Usage Guide
Astro使用指南
Always consult docs.astro.build for code examples and latest API.
Astro is the web framework for content-driven websites.
Quick Reference
快速参考
File Location
文件位置
CLI looks for , , , and in: . Use for custom path.
astro.config.jsastro.config.mjsastro.config.cjsastro.config.ts./--configCLI会在当前目录中查找、、和文件。使用参数指定自定义路径。
./astro.config.jsastro.config.mjsastro.config.cjsastro.config.ts--configCLI Commands
CLI命令
- - Start the development server.
npx astro dev - - Build your project and write it to disk.
npx astro build - - Check your project for errors.
npx astro check - - Add an integration.
npx astro add - - Generate TypeScript types for all Astro modules.
npmx astro sync
Re-run after adding/changing plugins.
- - 启动开发服务器。
npx astro dev - - 构建项目并写入磁盘。
npx astro build - - 检查项目中的错误。
npx astro check - - 添加集成。
npx astro add - - 为所有Astro模块生成TypeScript类型。
npmx astro sync
添加/更改插件后请重新运行。
Project Structure
项目结构
Astro leverages an opinionated folder layout for your project. Every Astro project root should include some directories and files. Reference project structure docs.
- - Your project source code (components, pages, styles, images, etc.)
src/* - - Required sub-directory in your Astro project. Without it, your site will have no pages or routes!
src/pages - - It is common to group and organize all of your project components together in this folder. This is a common convention in Astro projects, but it is not required. Feel free to organize your components however you like!
src/components - - Just like
src/layouts, this directory is a common convention but not required.src/components - - It is a common convention to store your CSS or Sass files here, but this is not required. As long as your styles live somewhere in the src/ directory and are imported correctly, Astro will handle and optimize them.
src/styles - - Your non-code, unprocessed assets (fonts, icons, etc.). The files in this folder will be copied into the build folder untouched, and then your site will be built.
public/* - - A project manifest.
package.json - - An Astro configuration file. (recommended)
astro.config.{js,mjs,cjs,ts} - - A TypeScript configuration file. (recommended)
tsconfig.json
Astro采用约定式的项目文件夹布局。每个Astro项目根目录应包含一些目录和文件。参考项目结构文档。
- - 项目源代码(组件、页面、样式、图片等)
src/* - - Astro项目中必需的子目录。没有它,你的网站将没有页面或路由!
src/pages - - 通常会将项目中的所有组件集中组织在此文件夹中。这是Astro项目的常见约定,但并非强制要求。你可以按照自己的喜好组织组件!
src/components - - 与
src/layouts类似,这个目录是常见约定但并非必需。src/components - - 通常会将CSS或Sass文件存储在此处,但这也不是强制要求。只要你的样式文件位于src/目录中且导入正确,Astro就会处理并优化它们。
src/styles - - 非代码类的未处理资源(字体、图标等)。此文件夹中的文件将原封不动地复制到构建文件夹中,然后再构建你的网站。
public/* - - 项目清单文件。
package.json - - Astro配置文件。(推荐使用)
astro.config.{js,mjs,cjs,ts} - - TypeScript配置文件。(推荐使用)
tsconfig.json
Core Config Options
核心配置选项
| Option | Notes |
|---|---|
| Your final, deployed URL. Astro uses this full URL to generate your sitemap and canonical URLs in your final build. |
| 选项 | 说明 |
|---|---|
| 最终部署的URL。Astro会使用这个完整URL在最终构建中生成站点地图和规范URL。 |
Adapters
适配器
Deploy to your favorite server, serverless, or edge host with build adapters. Use an adapter to enable on-demand rendering in your Astro project.
Add Node.js adapter using astro add:
npx astro add node --yesAdd Cloudflare adapter using astro add:
npx astro add cloudflare --yesAdd Netlify adapter using astro add:
npx astro add netlify --yesAdd Vercel adapter using astro add:
npx astro add vercel --yes通过构建适配器,你可以将项目部署到你喜爱的服务器、无服务器或边缘托管平台。使用适配器可在Astro项目中启用按需渲染。
使用astro add添加Node.js适配器:
npx astro add node --yes使用astro add添加Cloudflare适配器:
npx astro add cloudflare --yes使用astro add添加Netlify适配器:
npx astro add netlify --yes使用astro add添加Vercel适配器:
npx astro add vercel --yes