useHydration is a built-in composable that provides a way to set data on the server side every time a new HTTP request is made and receive that data on the client side. This way useHydration allows you to take full control of the hydration cycle.
useHydration 的设计目的是用于 确保 SSR 期间的状态同步与恢复。如果你需要在 Nuxt 中创建一个全局的、对 SSR 友好的响应式状态,推荐使用 useState。服务器上 get 函数返回的数据会根据作为 useHydration 第一个参数提供的唯一键,存储在 nuxtApp.payload 中。在水合过程中,这些数据会在客户端被检索,从而避免重复计算或 API 调用。
export default defineNuxtPlugin((nuxtApp) => {
const myStore = new MyStore()
useHydration(
'myStoreState',
() => myStore.getState(),
data => myStore.setState(data),
)
})
export default defineNuxtPlugin((nuxtApp) => {
const myStore = new MyStore()
if (import.meta.server) {
nuxt.hooks.hook('app:rendered', () => {
nuxtApp.payload.myStoreState = myStore.getState()
})
}
if (import.meta.client) {
nuxt.hooks.hook('app:created', () => {
myStore.setState(nuxtApp.payload.myStoreState)
})
}
})
export function useHydration<T> (key: string, get: () => T, set: (value: T) => void): void
| 参数 | 类型 | 描述 |
|---|---|---|
key | string | 唯一键,用于标识 Nuxt 应用中的数据。 |
get | () => T | 仅在服务器上执行的函数(在 SSR 渲染完成时调用),用于设置初始值。 |
set | (value: T) => void | 仅在客户端执行的函数(在初始 Vue 实例创建时调用),用于接收数据。 |
此可组合项不返回任何值。