跳到主要内容

createSharedComposable

分类
导出大小
232 B
上次更改
上个月
相关

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

警告

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

用法

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
()

类型声明

ts
export type 
SharedComposableReturn
<
T
extends
AnyFn
=
AnyFn
> =
T
/** * Make a composable function usable with multiple Vue instances. * * @see https://vueuse.org.cn/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!: 客户端只返回共享的 Composable (#4997)
v13.6.0
d32f8 - refactor: 为所有纯函数添加 @__NO_SIDE_EFFECTS__ 注释 (#4907)
v13.1.0
c1d6e - feat(shared): 确保返回类型存在 (#4659)
0a9ed - feat!: 放弃对 Vue 2 的支持,优化打包并清理 (#4349)
v11.0.2
a2441 - fix: 处理可组合函数返回值为空的情况 (#4151)

根据 MIT 许可证发布。