【发布时间】:2021-05-27 21:30:25
【问题描述】:
版本
- nuxt:^2.14.12
- 节点:v14.15.4
复制
大家好,提前谢谢你们。
我有一个奇怪的问题,我真的不明白问题是什么以及如何处理它。
我已经安装了一个新的 nuxt ssr 项目。
我收到以下警告
[Vue warn]: The client-side rendered virtual DOM tree is not matching server-rendered content. This is likely caused by incorrect HTML markup, for example nesting block-level elements inside <p>, or missing <tbody>. Bailing hydration and performing full client-side render.
我有三个简单的组件:Form、Input、Button。
Form.vue
<template>
<form v-bind="$attrs" class="w-full" @submit.prevent="$emit('submitted')">
<div class="space-y-2 mb-4">
<slot name="fields" />
</div>
<slot name="button" />
</div>
</form>
</template>
<script>
export default {
computed: {
hasFields() {
return !!this.$slots.fields
},
},
}
</script>
Input.vue
<template>
<div class="relative w-full">
<input class="form-input block w-full" />
</div>
</template>
<script>
export default {
inheritAttrs: false,
}
</script>
Button.vue
<template>
<button
type="submit"
class="relative btn inline-flex items-center justify-center transition ease-in-out duration-150"
>
Save
</button>
</template>
<script>
export default {}
</script>
我在pages/index.vue 中使用我的组件,如下所示:
<template>
<div>
<Form>
<template #fields>
<Input />
<Input />
</template>
<template #button>
<Button />
</template>
</Form>
<Form>
<template #fields>
<Input />
<Input />
</template>
<template #button>
<Button />
</template>
</Form>
</div>
</template>
<script>
export default {}
</script>
如果我在视图中只使用一次Form 组件,我不会收到警告。
如果我使用它两次,我就会得到它。
重现步骤
- 安装一个新的 nuxt ssr 项目。
- 在复制链接中创建组件
预期什么?
所有组件正常渲染,没有任何警告或错误。
实际发生了什么?
我收到以下警告。
[Vue warn]: The client-side rendered virtual DOM tree is not matching server-rendered content. This is likely caused by incorrect HTML markup, for example nesting block-level elements inside <p>, or missing <tbody>. Bailing hydration and performing full client-side render.
一些额外说明
- 我知道将整个内容包含在
<client-only>中可以解决问题,但我想了解为什么会发生这种情况,以便在以后的情况下避免这种情况。 - 此外,如果我从
nuxt.config.js中删除components: true并再次正常导入组件,警告就会消失。 - 更改组件的名称,例如
Button->TheButton不会解决问题。你可以看到reproduction here。
<script>
import Input from '~/components/Input'
import Button from '~/components/Button'
import Form from '~/components/Form'
export default {
components: { Form, Button, Input}
}
</script>
【问题讨论】:
-
我认为似乎有一个或多个组件在“通用”模式下不受支持。您只需要通过试错法找到该组件,并用标签
包装它。这是相同的链接:nuxtjs.org/docs/2.x/features/…
标签: vue.js nuxt.js server-side-rendering