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