在NuxtPage中不能使用布局中的命名插槽。

huangapple go评论57阅读模式
英文:

Named slots in layout can not be used within NuxtPage

问题

我认为我遗漏了一些基本的东西...

我想使用像官方文档中描述的布局功能一样的功能(链接:https://nuxt.com/docs/guide/directory-structure/layouts#setting-another-layout)
我的布局文件具有用于页眉和页脚的命名插槽。这些插槽需要从页面内部进行引用。

app.vue

<template>
    <NuxtLayout>
        <NuxtPage />
    </NuxtLayout>
</template>

layouts/default.vue

<template>
    <header>
        <slot name="header" />
    </header>
    <main>
        <slot />
    </main>
    <footer>
        <slot name="footer" />
    </footer>
</template>

pages/index.vue

<template>
    <template #header>
        HEADER
    </template>
</template>

我收到这个错误消息

[plugin:vite:vue] Codegen node is missing for element/if/for node. Apply appropriate transforms first.

我认为这是因为我将模板标记放在页面组件的根位置,但Google说模板标记需要放在根级别

英文:

I think I'm missing something essential..

I want to use the layout functionality like it is described in the official docs
My layout file has named slots for header and footer. These slots need to be addressed from within the page.

app.vue

&lt;template&gt;
    &lt;NuxtLayout&gt;
        &lt;NuxtPage /&gt;
    &lt;/NuxtLayout&gt;
&lt;/template&gt;

layouts/default.vue

&lt;template&gt;
    &lt;header&gt;
        &lt;slot name=&quot;header&quot; /&gt;
    &lt;/header&gt;
    &lt;main&gt;
        &lt;slot /&gt;
    &lt;/main&gt;
    &lt;footer&gt;
        &lt;slot name=&quot;footer&quot; /&gt;
    &lt;/footer&gt;
&lt;/template&gt;

pages/index.vue

&lt;template&gt;
    &lt;template #header&gt;
        HEADER
    &lt;/template&gt;
&lt;/template&gt;

I'm getting this error

[plugin:vite:vue] Codegen node is missing for element/if/for node. Apply appropriate transforms first.

I think thats because i have the template tag as the root of the page component, but google says that the template tags need to be on the root level

答案1

得分: 1

这是如何使/pages/index.vue工作并渲染头部插槽中的文本:

<template>
  <div>
    <NuxtLayout name="default">
      <template #header> HEADER </template>
    </NuxtLayout>
  </div>
</template>

但我不确定这是否是您真正想要做的。如果您想要一个通用的页眉和页脚,最好将此代码直接放在/layouts/default.vue中,或者创建一个组件并在那里使用它。

我为您准备了一个快速示例,演示如何设置具有通用页眉和页脚的布局:
https://stackblitz.com/edit/github-kzomp4

还请注意,除非您真的需要在应用程序中切换多个不同的布局,Nuxt 开发者建议不要干扰布局,而是将代码直接放在app.vue中。但这取决于您的实际用例。

英文:

This his how you make /pages/index.vue work and render the text in the header slot:

&lt;template&gt;
  &lt;div&gt;
    &lt;NuxtLayout name=&quot;default&quot;&gt;
      &lt;template #header&gt; HEADER &lt;/template&gt;
    &lt;/NuxtLayout&gt;
  &lt;/div&gt;
&lt;/template&gt;

But I am not sure wheter this is what you really want to do. If you want to have a common header and footer, it is better to put this code right inside /layouts/default.vue or make a component and use it there.

I prepared a quick example of how I would setup layout with common header and footer:
https://stackblitz.com/edit/github-kzomp4

Also note that unless you really need to switch between more than one different layout in your application, Nuxt developers recommend not messing with layouts at all and put the code right into app.vue. But that depends on your real use-case.

huangapple
  • 本文由 发表于 2023年2月14日 21:42:42
  • 转载请务必保留本文链接:https://go.coder-hub.com/75448712.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定