<!DOCTYPE html><html lang="zh-CN"> <head><meta charset="UTF-8"><meta name="viewport" content="width=device-width"><link rel="icon" type="image/svg+xml" href="/favicon.svg"><meta name="generator" content="Astro v5.18.2"><title>Astro 入门指南 | Astro 示范</title><meta name="description" content="从零开始了解 Astro，掌握现代前端框架的核心概念。"><link rel="stylesheet" href="/styles/global.css"><style>*,*:before,*:after{box-sizing:border-box;margin:0;padding:0}:root{--color-primary: #4f46e5;--color-primary-hover: #4338ca;--color-bg: #f8fafc;--color-surface: #ffffff;--color-text: #1e293b;--color-text-muted: #64748b;--color-border: #e2e8f0;--radius: 8px;--shadow: 0 1px 3px rgba(0, 0, 0, .1);--shadow-md: 0 4px 12px rgba(0, 0, 0, .1)}html{font-family:Inter,Noto Sans SC,system-ui,-apple-system,sans-serif;font-size:16px;line-height:1.6;color:var(--color-text);background:var(--color-bg)}body{min-height:100vh;display:flex;flex-direction:column}.container{max-width:960px;margin:0 auto;padding:0 1.5rem}.site-header{background:var(--color-surface);border-bottom:1px solid var(--color-border);padding:1rem 0;position:sticky;top:0;z-index:100;backdrop-filter:blur(8px)}.site-header .container{display:flex;justify-content:space-between;align-items:center}.logo{font-size:1.25rem;font-weight:700;text-decoration:none;color:var(--color-text)}nav{display:flex;gap:1.5rem}nav a{text-decoration:none;color:var(--color-text-muted);font-weight:500;transition:color .2s;padding:.25rem 0;border-bottom:2px solid transparent}nav a:hover{color:var(--color-primary);border-bottom-color:var(--color-primary)}main.container{flex:1;padding-top:3rem;padding-bottom:3rem}.site-footer{background:var(--color-surface);border-top:1px solid var(--color-border);padding:2rem 0;text-align:center;color:var(--color-text-muted);font-size:.875rem}.site-footer a{color:var(--color-primary);text-decoration:none}.site-footer a:hover{text-decoration:underline}h1{font-size:2.5rem;font-weight:800;line-height:1.2;margin-bottom:1rem}h2{font-size:1.75rem;font-weight:700;margin-bottom:1rem}h3{font-size:1.25rem;font-weight:600;margin-bottom:.5rem}p{margin-bottom:1rem;color:var(--color-text-muted)}
.post-header[data-astro-cid-4sn4zg3r]{text-align:center}.post-header[data-astro-cid-4sn4zg3r] h1[data-astro-cid-4sn4zg3r]{font-size:2.25rem;margin-bottom:.75rem}.post-meta[data-astro-cid-4sn4zg3r]{display:flex;justify-content:center;align-items:center;gap:1rem;flex-wrap:wrap;color:var(--color-text-muted);font-size:.9rem}.post-tags[data-astro-cid-4sn4zg3r]{display:flex;gap:.4rem}@media(max-width:640px){.post-header[data-astro-cid-4sn4zg3r] h1[data-astro-cid-4sn4zg3r]{font-size:1.75rem}}
</style></head> <body> <header class="site-header"> <div class="container"> <a href="/" class="logo">🚀 Astro 示范</a> <nav> <a href="/">首页</a> <a href="/about">关于</a> <a href="/blog">博客</a> <a href="/news">新闻</a> </nav> </div> </header> <main class="container">  <article data-astro-cid-4sn4zg3r> <div class="post-header" data-astro-cid-4sn4zg3r> <h1 data-astro-cid-4sn4zg3r>Astro 入门指南</h1> <div class="post-meta" data-astro-cid-4sn4zg3r> <span data-astro-cid-4sn4zg3r>📅 2026/6/15</span> <span data-astro-cid-4sn4zg3r>✍️ Astro 团队</span> <div class="post-tags" data-astro-cid-4sn4zg3r> <span class="tag" data-astro-cid-4sn4zg3r>入门</span><span class="tag" data-astro-cid-4sn4zg3r>指南</span><span class="tag" data-astro-cid-4sn4zg3r>基础</span> </div> </div> </div> <hr class="divider" data-astro-cid-4sn4zg3r> <div class="prose" data-astro-cid-4sn4zg3r> <h1 id="astro-入门指南">Astro 入门指南</h1>
<h2 id="什么是-astro">什么是 Astro？</h2>
<p>Astro 是一个<strong>现代静态站点生成器</strong>，它的核心理念是”按需发送 JavaScript”。</p>
<blockquote>
<p>💡 Astro 不会限制你使用任何特定的 UI 框架。你可以自由选择 React、Vue、Svelte、SolidJS 或只是纯 HTML。</p>
</blockquote>
<h2 id="为什么选择-astro">为什么选择 Astro？</h2>
<h3 id="1-默认零-javascript">1. 默认零 JavaScript</h3>
<p>在 Astro 中，所有组件默认为静态 HTML。这意味着：</p>
<ul>
<li>⚡ 页面加载速度极快</li>
<li>📉 用户可以更快地看到内容</li>
<li>🔒 更少的安全风险</li>
</ul>
<h3 id="2-岛屿架构">2. 岛屿架构</h3>
<pre class="astro-code github-dark" style="background-color:#24292e;color:#e1e4e8; overflow-x: auto;" tabindex="0" data-language="astro"><code><span class="line"><span style="color:#6A737D">---</span></span>
<span class="line"><span style="color:#6A737D">// 纯静态组件 — 零 JS</span></span>
<span class="line"><span style="color:#F97583">import</span><span style="color:#E1E4E8"> StaticCard </span><span style="color:#F97583">from</span><span style="color:#9ECBFF"> './StaticCard.astro'</span><span style="color:#E1E4E8">;</span></span>
<span class="line"></span>
<span class="line"><span style="color:#6A737D">// 交互式组件 — 仅在客户端加载 JS</span></span>
<span class="line"><span style="color:#F97583">import</span><span style="color:#E1E4E8"> InteractiveChart </span><span style="color:#F97583">from</span><span style="color:#9ECBFF"> './Chart.client.tsx'</span><span style="color:#E1E4E8">;</span></span>
<span class="line"><span style="color:#6A737D">---</span></span>
<span class="line"></span>
<span class="line"><span style="color:#E1E4E8">&#x3C;</span><span style="color:#85E89D">div</span><span style="color:#E1E4E8">></span></span>
<span class="line"><span style="color:#E1E4E8">  &#x3C;</span><span style="color:#79B8FF">StaticCard</span><span style="color:#E1E4E8"> /></span></span>
<span class="line"><span style="color:#E1E4E8">  &#x3C;</span><span style="color:#79B8FF">InteractiveChart</span><span style="color:#B392F0"> client:visible</span><span style="color:#E1E4E8"> /></span></span>
<span class="line"><span style="color:#E1E4E8">&#x3C;/</span><span style="color:#85E89D">div</span><span style="color:#E1E4E8">></span></span></code></pre>
<h3 id="3-内容集合">3. 内容集合</h3>
<p>Astro 内置了强大的内容管理系统：</p>
<pre class="astro-code github-dark" style="background-color:#24292e;color:#e1e4e8; overflow-x: auto;" tabindex="0" data-language="typescript"><code><span class="line"><span style="color:#6A737D">// src/content/config.ts</span></span>
<span class="line"><span style="color:#F97583">import</span><span style="color:#E1E4E8"> { defineCollection, z } </span><span style="color:#F97583">from</span><span style="color:#9ECBFF"> "astro:content"</span><span style="color:#E1E4E8">;</span></span>
<span class="line"></span>
<span class="line"><span style="color:#F97583">const</span><span style="color:#79B8FF"> blog</span><span style="color:#F97583"> =</span><span style="color:#B392F0"> defineCollection</span><span style="color:#E1E4E8">({</span></span>
<span class="line"><span style="color:#E1E4E8">  schema: z.</span><span style="color:#B392F0">object</span><span style="color:#E1E4E8">({</span></span>
<span class="line"><span style="color:#E1E4E8">    title: z.</span><span style="color:#B392F0">string</span><span style="color:#E1E4E8">(),</span></span>
<span class="line"><span style="color:#E1E4E8">    date: z.</span><span style="color:#B392F0">date</span><span style="color:#E1E4E8">(),</span></span>
<span class="line"><span style="color:#E1E4E8">    tags: z.</span><span style="color:#B392F0">array</span><span style="color:#E1E4E8">(z.</span><span style="color:#B392F0">string</span><span style="color:#E1E4E8">()),</span></span>
<span class="line"><span style="color:#E1E4E8">  }),</span></span>
<span class="line"><span style="color:#E1E4E8">});</span></span>
<span class="line"></span>
<span class="line"><span style="color:#F97583">export</span><span style="color:#F97583"> const</span><span style="color:#79B8FF"> collections</span><span style="color:#F97583"> =</span><span style="color:#E1E4E8"> { blog };</span></span></code></pre>
<h2 id="项目结构">项目结构</h2>
<pre class="astro-code github-dark" style="background-color:#24292e;color:#e1e4e8; overflow-x: auto;" tabindex="0" data-language="plaintext"><code><span class="line"><span>my-astro-site/</span></span>
<span class="line"><span>├── public/          # 静态资源（原样输出）</span></span>
<span class="line"><span>├── src/</span></span>
<span class="line"><span>│   ├── components/  # UI 组件</span></span>
<span class="line"><span>│   ├── content/     # 内容集合</span></span>
<span class="line"><span>│   ├── layouts/     # 页面布局</span></span>
<span class="line"><span>│   ├── pages/       # 路由页面</span></span>
<span class="line"><span>│   └── styles/      # 样式文件</span></span>
<span class="line"><span>└── astro.config.ts  # 配置文件</span></span></code></pre>
<h2 id="快速开始">快速开始</h2>
<ol>
<li><strong>创建项目</strong></li>
</ol>
<pre class="astro-code github-dark" style="background-color:#24292e;color:#e1e4e8; overflow-x: auto;" tabindex="0" data-language="bash"><code><span class="line"><span style="color:#B392F0">npm</span><span style="color:#9ECBFF"> create</span><span style="color:#9ECBFF"> astro@latest</span></span></code></pre>
<ol start="2">
<li><strong>启动开发服务器</strong></li>
</ol>
<pre class="astro-code github-dark" style="background-color:#24292e;color:#e1e4e8; overflow-x: auto;" tabindex="0" data-language="bash"><code><span class="line"><span style="color:#B392F0">npm</span><span style="color:#9ECBFF"> run</span><span style="color:#9ECBFF"> dev</span></span></code></pre>
<ol start="3">
<li><strong>构建生产版本</strong></li>
</ol>
<pre class="astro-code github-dark" style="background-color:#24292e;color:#e1e4e8; overflow-x: auto;" tabindex="0" data-language="bash"><code><span class="line"><span style="color:#B392F0">npm</span><span style="color:#9ECBFF"> run</span><span style="color:#9ECBFF"> build</span></span></code></pre>
<h2 id="最佳实践">最佳实践</h2>
<ul>
<li>📦 将交互式组件放在 <code>.client.tsx</code> / <code>.client.vue</code> 文件中</li>
<li>🎨 使用 <code>is:global</code> 给全局样式（如 CSS reset）</li>
<li>📝 用内容集合管理 Markdown 内容</li>
<li>🏗️ 善用布局组件减少重复代码</li>
<li>⚡ 优先使用 <code>client:idle</code> 或 <code>client:visible</code> 而非 <code>client:load</code></li>
</ul>
<h2 id="总结">总结</h2>
<p>Astro 的核心哲学是<strong>内容优先、按需交互</strong>。它让你能够构建极速的网站，同时保留使用你喜欢的任何工具和框架的自由。</p> </div> </article>  </main> <footer class="site-footer"> <div class="container"> <p>使用 <a href="https://astro.build" target="_blank" rel="noopener">Astro</a> 构建</p> </div> </footer> </body></html> 