我有一个在 SSR 中渲染得很好的组件,但是当 Vue 3 进行水合时会闪烁(我认为)。
<template>
<ul class="grid grid-cols-1 gap-6 sm:grid-cols-2 md:grid-cols-3 lg:grid-cols-4">
<li v-for="recipe in recipes" :key="recipe.id" class="col-span-1 flex flex-col text-center bg-white rounded-lg shadow divide-y divide-gray-200">
<div class="flex-1 flex flex-col">
<img class="w-full flex-shrink-0 mx-auto bg-black rounded-t-lg" :src="recipe.imageUrl" alt="" />
<h3 class="text-gray-900 text-sm font-medium p-4">
{{ recipe.title }}
</h3>
</div>
</li>
</ul>
<Pagination v-model="page" :records="totalRecords" :per-page="12" :options="paginationOptions" @paginate="fetchRecipes" />
</template>
<script>
import Pagination from 'v-pagination-3'
import axios from 'axios'
export default {
components: {
Pagination,
},
inject: ['paginationOptions'],
data() {
return {
section: 'home',
recipes: [],
page: 3,
totalRecords: 0,
}
},
created() {
},
mounted() {
this.fetchRecipes(this.page)
},
methods: {
async fetchRecipes(page) {
try {
const url = 'http://local-api.local/api/fakeApi'
const response = await axios.get(url, { params: { page } }).then((response) => {
this.recipes = response.data.data.map(recipe => ({
title: recipe.title,
imageUrl: `http://test-server.local${recipe.thumbnailUrl}`,
}))
this.totalRecords = response.data.total
})
}
catch (err) {
if (err.response) {
// client received an error response (5xx, 4xx)
console.log('Server Error:', err)
}
else if (err.request) {
// client never received a response, or request never left
console.log('Network Error:', err)
}
else {
console.log('Client Error:', err)
}
}
},
},
serverPrefetch() {
return this.fetchRecipes(this.page)
},
}
</script>
我究竟做错了什么?我一定已经尝试了 50 种设置方式this.recipes
(在所有可能的生命周期挂钩中),但所有这些方式仍然会导致水合闪烁。
我正在使用 Vite(如果重要,可以使用 vite-ssr 插件)/Vue 3 SSR。请注意,闪烁后的组件似乎与 SSR 生成的版本相同,该版本在页面加载时显示(并且在源代码中)。