【问题标题】:Put some elements from component into head tag将组件中的一些元素放入 head 标签中
【发布时间】:2023-01-29 20:34:05
【问题描述】:

我希望组件中的一些标签转到head 标签。 我想输入名为head 的slot。 怎么能做到这一点?

布局.astro

<html>
    <head>
        <slot name="head" />
    </head>
    <body>
        <slot />
        <mycomponent />
    </body>
</html>

我的组件.astro

<link .... slot="head">
<div>
    ...
</div> 

【问题讨论】:

    标签: astrojs


    【解决方案1】:

    可以放置一个标签,但只能从您调用放置单个 &lt;head&gt; 的布局的地方使用 slots 像这样

    在 Layout.astro 中创建一个

    • 默认插槽和
    • 一个命名槽,例如name="head" 但你可以使用任何名称
    
        <head>
            <title>{title}</title>
            <slot name="head"/>
        </head>
        
        <body>
            <slot />
        </body>
    

    然后在您的页面或调用布局的组件中

    
    <Layout title="Welcome to Astro.">
        <link slot="head" rel="icon" type="image/svg+xml" href="/favicon.svg" />
        <main>
            <h1>Astro <span class="text-gradient">Dynamic</span>Imports</h1>
            <a href={`https://github.com/MicroWebStacks/astro-examples/tree/main/${proj}`} target="_blank">github project '{proj}'</a>
            <p class="instructions"> Dynamic import only loads js when the components decides to, here after 2 seconds</p>
            <ul>
                <Card title="Test"/>
            </ul>
        </main>
    </Layout>
    

    参考

    注意:下面来自 Astro 文档网站的参考建议“将单曲及其内容放在布局组件中”。

    https://docs.astro.build/en/guides/troubleshooting/#using-head-in-a-component

    命名插槽:https://docs.astro.build/en/core-concepts/astro-components/#named-slots

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-10-20
      • 1970-01-01
      • 2016-10-17
      • 1970-01-01
      相关资源
      最近更新 更多