跳到主要內容

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 授權條款發布。