javascript - v-for throw 渲染的来自 AsyncData/fetch 的数组列表:客户端渲染的虚拟 DOM 树与服务器渲染的不匹配
问题描述
我正在为我的应用程序使用 Nuxt.js。在我的一个页面中,我使用AsyncData
从我的 Api 异步获取一组数据对象,我使用v-for
. 当我不使用nuxt-link
或<a href=".."></a>
在. v-for
只要我包含nuxt-link
或包含<a href=".."></a>
在我的v-for
中,我就会收到以下错误:
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.
我尝试完成的一个基本示例:
<template>
<div>
<div
class="place-card"
:key="place._id"
v-for="place in places"
>
<nuxt-link
:to="{
name: 'places-title',
params: { title: place.title }
}"
>
<PlaceCard :place="place" />
</nuxt-link>
</div>
</div>
</template>
<script>
export default {
asyncData() {
// Some preprocessing
return { places: [{...}, ..., {...}] }
}
}
</script>
v-for
当我将整个div 包装到@Mohsens 答案以及此处<client-only>...</client-only>
描述的此处时,我能够使其正常工作。
不幸的是,<client-only></client-only>
它阻止了异步数据的服务器端呈现。但是,由于 SEO,我确实需要在服务器端预取我的数据。
有谁知道这个问题的另一种解决方案?
编辑 12.10.2020 扩展日志
组件原代码PlaceCard
:
<template>
<div class="bg-white rounded overflow-hidden shadow-lg">
<img :src="place.images[0]" :alt="place.title" class="w-full h-40" />
<div class="px-6 pt-4">
<div class="font-bold text-base">{{ place.title }}</div>
</div>
<div class="px-6 pb-4">
<nuxt-link :to="'/'"
>#{{ place.placeType.split(' ').join('') }}</nuxt-link
>
<nuxt-link :to="'/'">#{{ place.address.country }}</nuxt-link>
<nuxt-link :to="'/'" v-if="place.vegan">#vegan</nuxt-link>
<nuxt-link :to="'/'" v-else>#not-vegan</nuxt-link>
<nuxt-link :to="'/'" v-if="place.vegetarian">#vegetarian</nuxt-link>
<nuxt-link :to="'/'" v-else>#not-vegetarian</nuxt-link>
</div>
<div class="author flex items-center py-3 px-6">
<div class="user-logo">
<img
class="w-8 h-8 object-cover rounded-full mr-2 shadow"
:src="place.creator.photoURL"
:alt="place.creator.displayName"
/>
</div>
<div class="block text-xs">
Added by<a href="#" class="ml-1">{{
place.creator.displayName.split(' ').join('')
}}</a>
</div>
</div>
</div>
</template>
<script>
export default {
props: {
place: {
type: Object,
default: null
}
}
}
</script>
解决方案
推荐阅读
- c# - How can I determine the length a windows login took for a user?
- node.js - Dockerizing Node.js 应用程序 - 做什么:ENV PATH /app/node_modules/.bin:$PATH
- python - Python Tkinter askopenfile 带输入框
- powerbi - PowerBI 无法在 parsePowerBIAccessToken 中的 'Window' 上执行 'atob'
- if-statement - 单个 if 语句中的 nil 检查和错误检查
- sql - PLS-00221:“SALE_TOTAL_AMOUNT”不是程序或未定义
- c# - NU1107:.Net Core 2.1 兼容的 Microsoft.EntityFrameworkCore.SqlServer 替代品?
- javascript - 获取 Firebase collectionGroup 中每个项目的父文档
- java - java语言规范上下文中的“类的二进制名称”是什么?
- powershell - 将 Get-ADReplicationStie 导入数组