Skip to content

createUnrefFn

Category
Export Size
166 B
Last Changed
last year
Related

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

TIP

确保您使用的是正确的工具。在某些情况下,使用 reactify 可能更加合适,特别是当您希望在参数发生变化时重新评估函数时。

用法

ts
import { ref } from 'vue'
import { createUnrefFn } from '@vueuse/core'

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) /* ❌ 将抛出错误,因为参数是 ref */
unrefPost(url, data) /* ✔️ 将正常工作,因为参数将自动解除引用 */

类型声明

typescript
export type UnrefFn<T> = T extends (...args: infer A) => infer R
  ? (
      ...args: {
        [K in keyof A]: MaybeRef<A[K]>
      }
    ) => R
  : never
/**
 * 创建一个普通函数,接受 ref 和原始值作为参数。
 * 返回与未转换函数返回相同的值,具有正确的类型。
 */
export declare function createUnrefFn<T extends Function>(fn: T): UnrefFn<T>

Source

SourceDocs

贡献者

Anthony Fu
丶远方
Stanley Horwood

变更日志

No recent changes

Released under the MIT License.

Build faster with AI
New Masterclass to help you leverage AI in your Vue workflow.
Get Early Access