Skip to content

createUnrefFn

类别
导出体积
101 B
上次更改
2 months ago
相关

创建一个普通函数,接受 ref 和原始值作为参数。 返回与未转换函数返回相同的值,具有正确的类型。

TIP

确保你使用的是正确的工具。在某些情况下,使用 reactify 可能更加合适,特别是当你希望在参数发生变化时重新评估函数时。

用法

ts
import { 
createUnrefFn
} from '@vueuse/core'
import {
shallowRef
} from 'vue'
const
url
=
shallowRef
('https://httpbin.org/post')
const
data
=
shallowRef
({
foo
: 'bar' })
function
post
(
url
,
data
) {
return
fetch
(
url
, {
data
})
} const
unrefPost
=
createUnrefFn
(
post
)
post
(
url
,
data
) /* ❌ 将抛出错误,因为参数是 ref */
unrefPost
(
url
,
data
) /* ✔️ 将正常工作,因为参数将自动解除引用 */

类型声明

ts
export type 
UnrefFn
<
T
> =
T
extends (...
args
: infer
A
) => infer
R
? ( ...
args
: {
[
K
in keyof
A
]:
MaybeRef
<
A
[
K
]>
} ) =>
R
: never /** * 创建一个普通函数,接受 ref 和原始值作为参数。 * 返回与未转换函数返回相同的值,具有正确的类型。 * * @__NO_SIDE_EFFECTS__ */ export declare function
createUnrefFn
<
T
extends Function>(
fn
:
T
):
UnrefFn
<
T
>

源码

源码文档

贡献者

Anthony Fu
一纸忘忧
Anthony Fu
IlyaL
SerKo
Stanley Horwood

更新日志

d32f8 - refactor: add @__NO_SIDE_EFFECTS__ annotations to all pure functions (#4907)
7432f - feat(types): deprecate MaybeRef and MaybeRefOrGetter in favor of Vue's native (#4636)
59f75 - feat(toValue): deprecate toValue from @vueuse/shared in favor of Vue's native