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 */
类型声明
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>