主题
createSharedComposable
使可组合函数可用于多个 Vue 实例。
¥Make a composable function usable with multiple Vue instances.
在 SSR 环境中使用时,[`createSharedComposable`](/shared/createSharedComposable/) 将自动回退到非共享版本。这意味着每次调用都会在 SSR 中创建一个新的实例,以避免 [跨请求状态污染](https://vuejs.org/guide/scaling-up/ssr.html#cross-request-state-pollution)。
¥[!WARNING] When used in a SSR environment, createSharedComposable
will automatically fallback to a non-shared version. This means every call will create a fresh instance in SSR to avoid cross-request state pollution.
用法
¥Usage
ts
import { createSharedComposable, useMouse } from '@vueuse/core'
const useSharedMouse = createSharedComposable(useMouse)
// CompA.vue
const { x, y } = useSharedMouse()
// CompB.vue - will reuse the previous state and no new event listeners will be registered
const { x, y } = useSharedMouse()