Nuxt3:水合失配(已解决)
最编程
2024-04-01 16:26:08
...
A hydration mismatch is when the content rendered to HTML on the server isn't the same as the content rendered in the browser.
Mismatch其实就是“不一致”,服务端渲染出来的html和浏览器渲染的html不一致
一般可能是这种写法导致:<span>{ new Date().getMilliseconds() }</span>
查了一些文档,上面说应该改成这种写法:
export { onBeforeRender }
async function onBeforeRender() {
const milliseconds = new Date().getMilliseconds()
return { pageContext: { milliseconds }
}
}
但是我用的nuxt3。。不知道放在哪个文件夹下面,就没用这种方法
从原理出发,不就是多渲染了一次吗?我于是把要渲染的数据用ClientOnly包裹了一下就不报错了,因为这样就避免渲染两次
对时间精度没啥要求,这样就解决了
还是不太懂为啥渲染两次..又回去看了一遍nuxt的文档-渲染模式
上一篇: [开源项目]智能鞋柜(STM32)
推荐阅读