跳到内容

createUnrefFn

类别
导出大小
153 B
上次更改
5 天前
相关

创建一个接受 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 */

类型声明

typescript
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.
 */
export declare function createUnrefFn<T extends Function>(fn: T): UnrefFn<T>

源码

源码文档

贡献者

Anthony Fu
Anthony Fu
IlyaL
Stanley Horwood

更新日志

v12.8.0 于 2025/3/5
7432f - feat(types): 弃用 MaybeRefMaybeRefOrGetter,转而使用 Vue 的原生类型 (#4636)
v12.3.0 于 2025/1/2
59f75 - feat(toValue): 弃用 @vueuse/shared 中的 toValue,转而使用 Vue 的原生类型

根据 MIT 许可证发布。