跳到主要内容

createUnrefFn

分类
导出大小
101 B
上次更改
3 个月前
相关

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

提示

请确保您正在为工作使用正确的工具。在某些情况下,当您希望在参数每次更改时都评估函数时,使用 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
) /* ❌ Will throw an error because the arguments are refs */
unrefPost
(
url
,
data
) /* ✔️ Will Work because the arguments will be auto unref */

类型声明

ts
export type 
UnrefFn
<
T
> =
T
extends (...
args
: infer
A
) => infer
R
? ( ...
args
: {
[
K
in keyof
A
]:
MaybeRef
<
A
[
K
]>
} ) =>
R
: never /** * Make a plain function accepting ref and raw values as arguments. * Returns the same value the unconverted function returns, with proper typing. * * @__NO_SIDE_EFFECTS__ */ export declare function
createUnrefFn
<
T
extends Function>(
fn
:
T
):
UnrefFn
<
T
>

来源

源代码文档

贡献者

Anthony Fu
Anthony Fu
IlyaL
SerKo
Stanley Horwood

更新日志

v13.6.0
d32f8 - refactor: 为所有纯函数添加 @__NO_SIDE_EFFECTS__ 注释 (#4907)
v12.8.0
7432f - feat(types): 废弃 MaybeRefMaybeRefOrGetter,转而使用 Vue 的原生类型 (#4636)
v12.3.0
59f75 - feat(toValue): 废弃 @vueuse/shared 中的 toValue,转而使用 Vue 的原生函数

根据 MIT 许可证发布。