主题
createUnrefFn
创建一个接受 ref 和原始值作为参数的普通函数。通过正确的类型返回与未转换的函数返回的值相同的值。
¥Make a plain function accepting ref and raw values as arguments. Returns the same value the unconverted function returns, with proper typing.
提示
确保你使用正确的工具来完成工作。在某些情况下,你希望根据参数的每次更改来评估函数,使用 reactify
可能更合适。
¥Make sure you're using the right tool for the job. Using reactify
might be more pertinent in some cases where you want to evaluate the function on each changes of it's arguments.
用法
¥Usage
ts
import { createUnrefFn } from '@vueuse/core'
import { ref } from 'vue'
const url = ref('https://httpbin.org/post')
const data = ref({ 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>