这是我的示例页面:
这是 Nuxt 布局:
<template>
<div>
<Nuxt />
</div>
</template>
这就是 Nuxt 编译器生成的结果:
如您所见,有几个无用的额外容器。 Nuxt 有什么方法可以摆脱它们吗?
如果几年后有人仍然遇到这个问题,您可以使用 Nuxt 3+(以及撰写本文时)来覆盖预期,您可以使用
rootTag
配置选项。
所以你的
nuxt.config.ts
可能看起来像这样:
export default defineNuxtConfig({
compatibilityDate: '2024-04-03',
app: {
rootTag: 'body',
rootId: 'app',
},
})
使用此配置将所有 HTML 元素放入
<body>
中,内容包含在主包装 div 中,以保持内容更加整洁。
至于
<template></template>
标签,我已经看过 Nuxt 示例,并且也以 0 个问题完成了它,但是您可以拥有多个根元素或删除包装 <div>
,这只是添加了另一层嵌套元素。<template>
<NuxtPage />
</template>