Skip to content

createSharedComposable

类别
导出体积
232 B
上次更改
6 minutes ago
相关

创建一个可在多个 Vue 实例中使用的可组合函数。

WARNING

SSR 环境中使用时,createSharedComposable自动回退 到非共享版本。 这意味着每个调用将在 SSR 中创建一个新的实例,以避免 跨请求状态污染

用法

ts
import { 
createSharedComposable
,
useMouse
} from '@vueuse/core'
const
useSharedMouse
=
createSharedComposable
(
useMouse
)
// CompA.vue const {
x
,
y
} =
useSharedMouse
()
// CompB.vue - 将重用先前的状态,不会注册新的事件监听器 const {
x
,
y
} =
useSharedMouse
()

类型声明

ts
export type 
SharedComposableReturn
<
T
extends
AnyFn
=
AnyFn
> =
T
/** * 使可组合函数可用于多个 Vue 实例。 * * @see https://vueuse.org/createSharedComposable * * @__NO_SIDE_EFFECTS__ */ export declare function
createSharedComposable
<
Fn
extends
AnyFn
>(
composable
:
Fn
,
):
SharedComposableReturn
<
Fn
>

源码

源码文档

贡献者

一纸忘忧
Anthony Fu
Vida Xie
SerKo
Robin
Anthony Fu
XinXiao
童欧巴
Egor Kotlyarov

更新日志

73aad - feat!: return only the sharedComposable on client side (#4997)
d32f8 - refactor: add @__NO_SIDE_EFFECTS__ annotations to all pure functions (#4907)
c1d6e - feat(shared): ensure return types exists (#4659)
0a9ed - feat!: drop Vue 2 support, optimize bundles and clean up (#4349)
a2441 - fix: handle the case where the return value of composable function is empty (#4151)