Skip to content

toRef

将 value/ref/getter 标准化为 refcomputed

¥Normalize value/ref/getter to ref or computed.

用法

¥Usage

ts
import { toRef } from '@vueuse/core'

const foo = ref('hi')

const a = toRef(0) // Ref<number>
const b = toRef(foo) // Ref<string>
const c = toRef(() => 'hi') // ComputedRef<string>

类型声明

typescript
/**
 * Normalize value/ref/getter to `ref` or `computed`.
 */
export declare function toRef<T>(r: () => T): Readonly<Ref<T>>
export declare function toRef<T>(r: ComputedRef<T>): ComputedRef<T>
export declare function toRef<T>(r: MaybeRefOrGetter<T>): Ref<T>
export declare function toRef<T>(r: T): Ref<T>
export declare function toRef<T extends object, K extends keyof T>(
  object: T,
  key: K,
): ToRef<T[K]>
export declare function toRef<T extends object, K extends keyof T>(
  object: T,
  key: K,
  defaultValue: T[K],
): ToRef<Exclude<T[K], undefined>>
/**
 * @deprecated use `toRef` instead
 */
export declare const resolveRef: typeof toRef

源代码

源代码文档

变更日志

v13.0.0 on 3/10/2025
5e046 - feat!: drop CJS build, now it's ESM-only (#4581)
v12.8.0 on 3/5/2025
7432f - feat(types): deprecate MaybeRef and MaybeRefOrGetter in favor of Vue's native (#4636)

VueUse v13.0 中文网 - 粤ICP备13048890号