Skip to content

构建你的第一个布局

准备好……

  • 将常见元素重构为页面布局

  • 使用 Astro <slot /> 元素将页面内容放置在布局中

  • 将特定于页面的值作为其布局的属性传递

你仍然有一些 Astro 组件在每个页面上重复渲染。是时候再次重构以创建共享页面布局了!

¥You still have some Astro components repeatedly rendered on every page. It’s time to refactor again to create a shared page layout!

创建你的第一个布局组件

标题部分 创建你的第一个布局组件

¥Create your first layout component

  1. Create a new file at the location src/layouts/BaseLayout.astro. (You will need to create a new layouts folder first.)

  2. Copy the entire contents of index.astro into your new file, BaseLayout.astro.

    src/layouts/BaseLayout.astro
    ---
    import Header from '../components/Header.astro';
    import Footer from '../components/Footer.astro';
    import '../styles/global.css';
    const pageTitle = "Home Page";
    ---
    <html lang="en">
    <head>
    <meta charset="utf-8" />
    <link rel="icon" type="image/svg+xml" href="/favicon.svg" />
    <meta name="viewport" content="width=device-width" />
    <meta name="generator" content={Astro.generator} />
    <title>{pageTitle}</title>
    </head>
    <body>
    <Header />
    <h1>{pageTitle}</h1>
    <Footer />
    <script>
    import "../scripts/menu.js";
    </script>
    </body>
    </html>

在页面上使用你的布局

标题部分 在页面上使用你的布局

¥Use your layout on a page

  1. Replace the code at src/pages/index.astro with the following:

    src/pages/index.astro
    ---
    import BaseLayout from '../layouts/BaseLayout.astro';
    const pageTitle = "Home Page";
    ---
    <BaseLayout>
    <h2>My awesome blog subtitle</h2>
    </BaseLayout>
  2. Check the browser preview again to notice what did (or, spoiler alert: did not!) change.

  3. Add a <slot /> element to src/layouts/BaseLayout.astro just above the footer component, then check the browser preview of your Home page and notice what really did change this time!

    src/layouts/BaseLayout.astro
    ---
    import Header from '../components/Header.astro';
    import Footer from '../components/Footer.astro';
    import '../styles/global.css';
    const pageTitle = "Home Page";
    ---
    <html lang="en">
    <head>
    <meta charset="utf-8" />
    <link rel="icon" type="image/svg+xml" href="/favicon.svg" />
    <meta name="viewport" content="width=device-width" />
    <meta name="generator" content={Astro.generator} />
    <title>{pageTitle}</title>
    </head>
    <body>
    <Header />
    <h1>{pageTitle}</h1>
    <slot />
    <Footer />
    <script>
    import "../scripts/menu.js";
    </script>
    </body>
    </html>

<slot /> 允许你将写在打开和关闭 <Component></Component> 标签之间的子内容注入(或 “插入”)到任何 Component.astro 文件。

¥The <slot /> allows you to inject (or “slot in”) child content written between opening and closing <Component></Component> tags to any Component.astro file.

将特定于页面的值作为属性传递

标题部分 将特定于页面的值作为属性传递

¥Pass page-specific values as props

  1. Pass the page title to your layout component from index.astro using a component attribute:

    src/pages/index.astro
    ---
    import BaseLayout from '../layouts/BaseLayout.astro';
    const pageTitle = "Home Page";
    ---
    <BaseLayout pageTitle={pageTitle}>
    <h2>My awesome blog subtitle</h2>
    </BaseLayout>
  2. Change the script of your BaseLayout.astro layout component to receive a page title via Astro.props instead of defining it as a constant.

    src/layouts/BaseLayout.astro
    ---
    import Header from '../components/Header.astro';
    import Footer from '../components/Footer.astro';
    import '../styles/global.css';
    const pageTitle = "Home Page";
    const { pageTitle } = Astro.props;
    ---
  3. Check your browser preview to verify that your page title has not changed. It has the same value, but is now being rendered dynamically. And now, each individual page can specify its own title to the layout.

自己尝试一下 - 随处使用你的布局

标题部分 自己尝试一下 - 随处使用你的布局

¥Try it yourself - Use your layout everywhere

重构其他页面(blog.astroabout.astro),以便它们使用你的新 <BaseLayout> 组件来渲染通用页面元素。

¥Refactor your other pages (blog.astro and about.astro) so that they use your new <BaseLayout> component to render the common page elements.

不要忘记:

¥Don’t forget to:

  • 通过组件属性将页面标题作为 props 传递。

  • 让布局负责任何常见元素的 HTML 渲染。

  • 将页面 <head> 中任何现有的 <style> 标签以及你希望保留的样式移动到页面 HTML 模板。

  • 从布局现在处理的每个单独页面中删除任何内容,包括:

    • HTML 元素

    • 零部件及其导入

    • <style> 标签中的 CSS 规则(例如“关于”页面中的 <h1>

    • <script> 标签

¥Test your knowledge

  1. Astro 组件(.astro 文件)可以用作:
  1. 要在页面上显示页面标题,你可以:
  1. 信息可以通过以下方式从一个组件传递到另一个组件:

¥Checklist

¥Resources

Astro 中文网 - 粤ICP备13048890号